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)


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

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:

  • name (String, Symbol)

    key



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

#attributesHash

Record attributes

Returns:

  • (Hash)


40
41
42
# File 'lib/dbf/record.rb', line 40

def attributes
  @attributes ||= Hash[column_names.zip(to_a)]
end

#match?(options) ⇒ Boolean

Do all search parameters match?

Parameters:

  • options (Hash)

Returns:

  • (Boolean)


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

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

#to_aArray

Maps a row to an array of values

Returns:

  • (Array)


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

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