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
12 13 14 15 16 17 |
# File 'lib/dbf/record.rb', line 12 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:
92 93 94 95 96 97 98 |
# File 'lib/dbf/record.rb', line 92 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
23 24 25 |
# File 'lib/dbf/record.rb', line 23 def ==(other) other.respond_to?(:attributes) && other.attributes == attributes end |
#[](name) ⇒ Object
Reads attributes by column name
30 31 32 33 34 35 36 37 |
# File 'lib/dbf/record.rb', line 30 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
42 43 44 |
# File 'lib/dbf/record.rb', line 42 def attributes @attributes ||= column_names.zip(to_a).to_h end |
#match?(options) ⇒ Boolean
Do all search parameters match?
50 51 52 |
# File 'lib/dbf/record.rb', line 50 def match?() .all? { |key, value| self[key] == value } end |
#to_a ⇒ Array
Maps a row to an array of values
57 58 59 |
# File 'lib/dbf/record.rb', line 57 def to_a @to_a ||= @columns.map { |column| init_attribute(column) } end |