Class: PackedStruct::Directive
- Inherits:
-
Object
- Object
- PackedStruct::Directive
- Defined in:
- lib/packed_struct/directive.rb
Overview
Contains information about a directive. A directive can be a name of the type, the endian(ness) of the type, the type of the type (short, int, long, etc.), and the signed(ness) of the type.
Instance Attribute Summary collapse
-
#name ⇒ Symbol
readonly
The name of the directive.
-
#options ⇒ Array<Object>
readonly
The arguments passed to the directive.
-
#parent ⇒ nil, Directive
The parent of this directive.
-
#subs ⇒ nil, Array<Directive>
readonly
The children of this directive.
-
#tags ⇒ Hash
The tags the directive has, such as type, signed(ness), endian(ness), and size.
- #value ⇒ Object
Instance Method Summary collapse
-
#+(other) ⇒ Directive
To show the size of something else, relative to this directive.
-
#-(other) ⇒ Directive
To show the size of something else, relative to this directive.
-
#[](size) ⇒ self
Set the size of this directive.
-
#add_child(child) ⇒ Directive
Add a child to this (or its parent’s) directive.
-
#bytesize ⇒ Numeric
The number of bytes this takes up in the resulting packed string.
-
#initialize(name, *arguments) ⇒ Directive
constructor
Initialize the directive.
-
#inspect ⇒ String
Inspects the directive.
-
#to_s ⇒ String
Turn the directive into a string.
Constructor Details
#initialize(name, *arguments) ⇒ Directive
Initialize the directive.
52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/packed_struct/directive.rb', line 52 def initialize(name, *arguments) @name = name @options = arguments @tags = {} @subs = [] @parent = nil @value = nil if arguments.first.is_a? Directive arguments.first.add_child(self) @subs = nil end end |
Instance Attribute Details
#name ⇒ Symbol (readonly)
The name of the directive. This is passed as the first value of the directive; from Package, it is the name of the method call.
12 13 14 |
# File 'lib/packed_struct/directive.rb', line 12 def name @name end |
#options ⇒ Array<Object> (readonly)
The arguments passed to the directive.
17 18 19 |
# File 'lib/packed_struct/directive.rb', line 17 def @options end |
#parent ⇒ nil, Directive
The parent of this directive. The relationship is such that parent.subs.include?(self) is true. If this has a parent, it is nil.
36 37 38 |
# File 'lib/packed_struct/directive.rb', line 36 def parent @parent end |
#subs ⇒ nil, Array<Directive> (readonly)
The children of this directive. If this directive has a parent, this is nil.
29 30 31 |
# File 'lib/packed_struct/directive.rb', line 29 def subs @subs end |
#tags ⇒ Hash
The tags the directive has, such as type, signed(ness), endian(ness), and size. Not filled until #to_s is called.
23 24 25 |
# File 'lib/packed_struct/directive.rb', line 23 def @tags end |
#value ⇒ Object
45 46 47 |
# File 'lib/packed_struct/directive.rb', line 45 def value @value || (@tags[:original].value if @tags[:original]) end |
Instance Method Details
#+(other) ⇒ Directive
To show the size of something else, relative to this directive.
128 129 130 131 132 133 134 |
# File 'lib/packed_struct/directive.rb', line 128 def +(other) dir = dup dir. = .dup dir.[:original ] = self dir.[:size_modify] = +other dir end |
#-(other) ⇒ Directive
To show the size of something else, relative to this directive.
117 118 119 120 121 122 123 |
# File 'lib/packed_struct/directive.rb', line 117 def -(other) dir = dup dir. = .dup dir.[:original ] = self dir.[:size_modify] = -other dir end |
#[](size) ⇒ self
Set the size of this directive.
88 89 90 91 |
# File 'lib/packed_struct/directive.rb', line 88 def [](size) @tags[:size] = size self end |
#add_child(child) ⇒ Directive
Add a child to this (or its parent’s) directive. If this is a child itself, it adds it to the parent of this. Invalidates the caches for #sub_names and #to_s
73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/packed_struct/directive.rb', line 73 def add_child(child) if @parent @parent.add_child(child) else @_str = nil @_sub_types = nil @subs << child child.parent = self child end end |
#bytesize ⇒ Numeric
The number of bytes this takes up in the resulting packed string.
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/packed_struct/directive.rb', line 139 def bytesize case @tags[:type] when nil size / 8 when :short 2 when :int 4 when :long 4 when :float if sub_names.include?(:double) 8 else 4 end when :null size || 1 when :string size else 0 end end |
#inspect ⇒ String
Inspects the directive.
110 111 112 |
# File 'lib/packed_struct/directive.rb', line 110 def inspect "#<#{self.class.name}:#{name}>" end |
#to_s ⇒ String
Turn the directive into a string. Analyzes the subs before determining information, then outputs that. Caches the value until #add_child is next called.
98 99 100 101 102 103 104 105 |
# File 'lib/packed_struct/directive.rb', line 98 def to_s return "" unless @subs @subs.compact! @tags[:signed] = determine_signed @tags[:type] = determine_type @tags[:endian] = determine_endian "#{make_directive}#{make_length}" end |