Class: ADT::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/adt/record.rb

Overview

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

Instance Method Summary collapse

Constructor Details

#initialize(data, columns, table) ⇒ Record

Initialize a new ADT::Record



8
9
10
11
12
13
# File 'lib/adt/record.rb', line 8

def initialize(data, columns, table)
  @data = ::StringIO.new(data)
  @data.seek(4) # We don't know what the first 4 bytes are
  @table = table
  @columns = columns
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object (private)

:nodoc:



72
73
74
75
76
77
78
# File 'lib/adt/record.rb', line 72

def method_missing(method, *args) # :nodoc:
  if (index = underscored_column_names.index(method.to_s))
    attributes[@columns[index].name]
  else
    super
  end
end

Instance Method Details

#==(other) ⇒ Boolean

Equality

Parameters:

Returns:

  • (Boolean)


53
54
55
# File 'lib/adt/record.rb', line 53

def ==(other)
  other.respond_to?(:attributes) && other.attributes == attributes
end

#[](name) ⇒ Object

Reads attributes by column name

Parameters:

  • key (String, Symbol)


40
41
42
43
44
45
46
47
# File 'lib/adt/record.rb', line 40

def [](name)
  key = name.to_s
  if attributes.key?(key)
    attributes[key]
  elsif index = underscored_column_names.index(key)
    attributes[@columns[index].name]
  end
end

#attributesHash

Record attributes

Returns:

  • (Hash)


18
19
20
# File 'lib/adt/record.rb', line 18

def attributes
  @attributes ||= Hash[attribute_map]
end

#match?(options) ⇒ Boolean

Do all search parameters match?

Parameters:

  • options (Hash)

Returns:

  • (Boolean)


33
34
35
# File 'lib/adt/record.rb', line 33

def match?(options)
  options.all? { |key, value| self[key] == value }
end

#to_aArray

Maps a row to an array of values

Returns:

  • (Array)


25
26
27
# File 'lib/adt/record.rb', line 25

def to_a
  @columns.map { |column| attributes[column.underscored_name] }
end