Class: DBF::Record

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

Overview

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

Instance Method Summary (collapse)

Constructor Details

- (Record) initialize(data, columns, version, memo)

Initialize a new DBF::Record

Version:

  • String


10
11
12
13
# File 'lib/dbf/record.rb', line 10

def initialize(data, columns, version, memo)
  @data = StringIO.new(data)
  @columns, @version, @memo = columns, version, memo
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

- (Object) method_missing(method, *args)



58
59
60
61
62
63
64
# File 'lib/dbf/record.rb', line 58

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

Instance Method Details

- (Boolean) ==(other)

Equality

Parameters:

Returns:

  • (Boolean)


19
20
21
# File 'lib/dbf/record.rb', line 19

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

- (Object) [](key)

Reads attributes by column name



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

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

- (Hash) attributes

Returns:

  • (Hash)


49
50
51
# File 'lib/dbf/record.rb', line 49

def attributes
  @attributes ||= Hash[@columns.map {|column| [column.name, init_attribute(column)]}]
end

- (Boolean) match?(options)

Do all search parameters match?

Parameters:

  • options (Hash)

Returns:

  • (Boolean)


34
35
36
# File 'lib/dbf/record.rb', line 34

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

- (Boolean) respond_to?(method, *args)

Returns:

  • (Boolean)


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

def respond_to?(method, *args)
  return true if column_names.include?(method.to_s)
  super
end

- (Array) to_a

Maps a row to an array of values

Returns:

  • (Array)


26
27
28
# File 'lib/dbf/record.rb', line 26

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