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

#initialize(data, columns, version, memo) ⇒ Record

Initialize a new DBF::Record

Version:

  • String


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

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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

nodoc



80
81
82
83
84
85
86
# File 'lib/dbf/record.rb', line 80

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)


21
22
23
# File 'lib/dbf/record.rb', line 21

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

#[](name) ⇒ Object

Reads attributes by column name

Parameters:

  • key (String, Symbol)


43
44
45
46
47
48
49
50
# File 'lib/dbf/record.rb', line 43

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)


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

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

#match?(options) ⇒ Boolean

Do all search parameters match?

Parameters:

  • options (Hash)

Returns:

  • (Boolean)


36
37
38
# File 'lib/dbf/record.rb', line 36

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

#respond_to?(method, *args) ⇒ Boolean

Overrides standard Object.respond_to? to return true if a matching column name is found.

Parameters:

  • method (String, Symbol)

Returns:

  • (Boolean)


64
65
66
# File 'lib/dbf/record.rb', line 64

def respond_to?(method, *args)
  underscored_column_names.include?(method.to_s) || super
end

#to_aArray

Maps a row to an array of values

Returns:

  • (Array)


28
29
30
# File 'lib/dbf/record.rb', line 28

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