class PackedStruct::Modifier

Public Class Methods

new(name) click to toggle source

Initializes the modifier.

# File lib/packed_struct/modifier.rb, line 5
def initialize(name)
  @name = name
  @type = nil
  @value = nil
end

Public Instance Methods

compile!() click to toggle source

Compiles the modifier into a usable format. Stores the values it determines in +@type+ and +@value+.

@raises [UnknownModifierError] if it can’t detect the type of

modifier.

@return [void]

# 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
type() click to toggle source

The type of modifier it is. Has four possible values: :endian, :size, :length, :type, :string_type, and :signedness.

@return [Array<Symbol>] @!parse

attr_reader :type
# File lib/packed_struct/modifier.rb, line 18
def type
  compile! unless @compiled
  [@type].flatten
end
value() click to toggle source

The value of the modifier. Has multiple possible values, including: :little, :big, :short, :int, :long, :float, :null, :string, :unsigned, :signed.

@return [Array<Symbol>] @!parse

attr_reader :value
# File lib/packed_struct/modifier.rb, line 30
def value
  compile! unless @compiled
  [@value].flatten
end