Class: PackedStruct::Modifier
- Inherits:
-
Object
- Object
- PackedStruct::Modifier
- Defined in:
- lib/packed_struct/modifier.rb
Instance Attribute Summary collapse
-
#type ⇒ Array<Symbol>
readonly
The type of modifier it is.
-
#value ⇒ Array<Symbol>
readonly
The value of the modifier.
Instance Method Summary collapse
-
#compile! ⇒ void
Compiles the modifier into a usable format.
-
#initialize(name) ⇒ Modifier
constructor
Initializes the modifier.
Constructor Details
#initialize(name) ⇒ Modifier
Initializes the modifier.
5 6 7 8 9 |
# File 'lib/packed_struct/modifier.rb', line 5 def initialize(name) @name = name @type = nil @value = nil end |
Instance Attribute Details
#type ⇒ Array<Symbol> (readonly)
The type of modifier it is. Has four possible values: :endian, :size, :length, :type, :string_type, and :signedness.
18 19 20 21 |
# File 'lib/packed_struct/modifier.rb', line 18 def type compile! unless @compiled [@type].flatten end |
#value ⇒ Array<Symbol> (readonly)
The value of the modifier. Has multiple possible values, including: :little, :big, :short, :int, :long, :float, :null, :string, :unsigned, :signed.
30 31 32 33 |
# File 'lib/packed_struct/modifier.rb', line 30 def value compile! unless @compiled [@value].flatten end |
Instance Method Details
#compile! ⇒ void
This method returns an undefined value.
Compiles the modifier into a usable format. Stores the values it determines in @type and @value.
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/packed_struct/modifier.rb', line 41 def compile! @compiled ||= begin case @name when :little_endian, :little, :lsb, :low @type = :endian @value = :little when :big_endian, :big, :msb, :high, :network @type = :endian @value = :big when :short, :int, :long, :float, :string @type = :type @value = @name when :unsigned, :signed @type = :signedness @value = @name when :null @type = :signedness @value = :signed when :spaced @type = :signedness @value = :unsigned when :double @type = :length @value = :double when :hex, :base64, :bit @type = :string_type @value = @name when :binary @type = :string_type @value = :bit when :uint8, :uint16, :uint32, :sint8, :sint16, :sint32, :int8, :int16, :int32 #/([us]?)int(8|16|32)/ @name.to_s =~ /([us]?)int(8|16|32)/ @type = [:signedness, :size] @value = [] if $1 == "u" @value.push(:unsigned) else @value.push(:signed) end @value.push($2.to_i) else raise UnknownModifierError, "Unkown modifier: #{@name}" end true end end |