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

Parameters:

  • data (String, StringIO)

    data

  • columns (Column)
  • version (String)
  • memo (DBF::Memo)


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

Parameters:

Returns:

  • (Boolean)


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

Parameters:

  • name (String, Symbol)

    key



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

#attributesHash

Record attributes

Returns:

  • (Hash)


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?

Parameters:

  • options (Hash)

Returns:

  • (Boolean)


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

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

#to_aArray

Maps a row to an array of values

Returns:

  • (Array)


57
58
59
# File 'lib/dbf/record.rb', line 57

def to_a
  @to_a ||= @columns.map { |column| init_attribute(column) }
end