class RADT::Record

An instance of RADT::Record represents a row in the RADT file

Attributes

attributes[R]

Public Class Methods

new(table) click to toggle source

Initialize a new ADT::Record

@param [RADT::Table] table

# File lib/radt/record.rb, line 13
def initialize(table)
  @table, @data = table, table.data

  initialize_values
  define_accessors
end

Public Instance Methods

==(other) click to toggle source

Equality

@param [RADT::Record] other @return [Boolean]

# File lib/radt/record.rb, line 24
def ==(other)
  other.respond_to?(:attributes) && other.attributes == attributes
end
to_a() click to toggle source

Maps a row to an array of values

@return [Array]

# File lib/radt/record.rb, line 31
def to_a
  columns.map { |column| @attributes[column.name.underscore] }
end

Private Instance Methods

define_accessors() click to toggle source

Defined attribute accessor methods

# File lib/radt/record.rb, line 38
def define_accessors
  columns.each do |column|
    underscored_column_name = column.name.underscore
    unless respond_to?(underscored_column_name)
      self.class.send :define_method, underscored_column_name do
        @attributes[column.name.underscore]
      end
    end
  end
end
initialize_values() click to toggle source

Initialize values for a row

# File lib/radt/record.rb, line 50
def initialize_values
  #skip the first 5 bytes, don't know what they are for and they don't contain the data.
  @data.read(5)

  @attributes = columns.inject({}) do |hash, column|

    #get the unpack flag to get this data.
    value = @data.read(column.length).unpack("#{column.flag(column.type, column.length)}").first
    hash[column.name] = value
    hash[column.name.underscore] = value

    hash
  end
end