A handful of fixes/enhancements for the "dbf" Rubygem.
require 'rubygems'
require 'dbf'
require 'dbf_fixes'
table = DBF::Table.new('/path/to/table.dbf', :in_memory => false)
table.each_record do |record|
end
module DBF
class Record
private
def initialize_values(columns)
columns.each do |column|
case column.type
when 'I'
@attributes[column.name] = @data.read(column.length).unpack("I").first
when 'N'
@attributes[column.name] = column.decimal.zero? ? unpack_string(column).to_i : unpack_string(column).to_f
when 'D'
raw = unpack_string(column).strip
unless raw.empty?
begin
parts = raw.match(DATE_REGEXP).to_a.slice(1,3).map {|n| n.to_i}
@attributes[column.name] = Time.gm(*parts)
rescue
parts = raw.match(DATE_REGEXP).to_a.slice(1,3).map {|n| n.to_i}
@attributes[column.name] = Date.new(*parts)
end
end
when 'M'
starting_block = unpack_string(column).to_i
@attributes[column.name] = read_memo(starting_block)
when 'L'
@attributes[column.name] = unpack_string(column) =~ /^(y|t)$/i ? true : false
else
@attributes[column.name] = unpack_string(column).strip
end
end
end
def define_accessors
@table.columns.each do |column|
underscored_column_name = underscore(column.name)
if @table.options[:accessors]
self.class.send :define_method, underscored_column_name do
@attributes[column.name]
end
@@accessors_defined = true
end
end
end
end
class Table
def each_record
if options[:in_memory] and @records
@records.each { |r| yield(r) }
else
0.upto(@record_count - 1) do |n|
seek_to_record(n)
yield(DBF::Record.new(self)) unless deleted_record?
end
end
end
end
end