Class: DBF::Record
- Inherits:
-
Object
- Object
- DBF::Record
- Defined in:
- lib/dbf/record.rb
Overview
An instance of DBF::Record represents a row in the DBF file
Instance Method Summary collapse
-
#==(other) ⇒ Boolean
Equality.
-
#[](name) ⇒ Object
Reads attributes by column name.
-
#attributes ⇒ Hash
Record attributes.
-
#initialize(data, columns, version, memo) ⇒ Record
constructor
Initialize a new DBF::Record.
-
#match?(options) ⇒ Boolean
Do all search parameters match?.
-
#to_a ⇒ Array
Maps a row to an array of values.
Constructor Details
#initialize(data, columns, version, memo) ⇒ Record
Initialize a new DBF::Record
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:
90 91 92 93 94 95 96 |
# File 'lib/dbf/record.rb', line 90 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
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
28 29 30 31 32 33 34 35 |
# File 'lib/dbf/record.rb', line 28 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 |
#attributes ⇒ Hash
Record attributes
40 41 42 |
# File 'lib/dbf/record.rb', line 40 def attributes @attributes ||= column_names.zip(to_a).to_h end |
#match?(options) ⇒ Boolean
Do all search parameters match?
48 49 50 |
# File 'lib/dbf/record.rb', line 48 def match?() .all? { |key, value| self[key] == value } end |
#to_a ⇒ Array
Maps a row to an array of values
55 56 57 |
# File 'lib/dbf/record.rb', line 55 def to_a @to_a ||= @columns.map { |column| init_attribute(column) } end |