class DBF::Table

DBF::Table is the primary interface to a single DBF file and provides methods for enumerating and searching the records.

Constants

DBASE2_HEADER_SIZE
DBASE3_HEADER_SIZE
DBASE7_HEADER_SIZE
FOXPRO_VERSIONS
VERSIONS

Attributes

encoding[RW]
name[W]

Public Class Methods

new(data, memo = nil, encoding = nil) { |self| ... } click to toggle source

Opens a DBF::Table Examples:

# working with a file stored on the filesystem
table = DBF::Table.new 'data.dbf'

# working with a misnamed memo file
table = DBF::Table.new 'data.dbf', 'memo.dbt'

# working with a dbf in memory
table = DBF::Table.new StringIO.new(dbf_data)

# working with a dbf and memo in memory
table = DBF::Table.new StringIO.new(dbf_data), StringIO.new(memo_data)

# working with a dbf overriding specified in the dbf encoding
table = DBF::Table.new 'data.dbf', nil, 'cp437'
table = DBF::Table.new 'data.dbf', 'memo.dbt', Encoding::US_ASCII

@param data [String, StringIO] data Path to the dbf file or a StringIO object @param memo [optional String, StringIO] memo Path to the memo file or a StringIO object @param encoding [optional String, Encoding] encoding Name of the encoding or an Encoding object

# File lib/dbf/table.rb, line 77
def initialize(data, memo = nil, encoding = nil)
  @data = open_data(data)
  @encoding = encoding || header.encoding
  @memo = open_memo(data, memo)
  yield self if block_given?
end

Public Instance Methods

close() click to toggle source

Closes the table and memo file

@return [TrueClass, FalseClass]

# File lib/dbf/table.rb, line 87
def close
  @data.close
  @memo && @memo.close
end
closed?() click to toggle source

@return [TrueClass, FalseClass]

# File lib/dbf/table.rb, line 93
def closed?
  if @memo
    @data.closed? && @memo.closed?
  else
    @data.closed?
  end
end
column_names() click to toggle source

Column names

@return [String]

# File lib/dbf/table.rb, line 104
def column_names
  @column_names ||= columns.map(&:name)
end
columns() click to toggle source

All columns

@return [Array]

# File lib/dbf/table.rb, line 111
def columns
  @columns ||= build_columns
end
each() { |record(i)| ... } click to toggle source

Calls block once for each record in the table. The record may be nil if the record has been marked as deleted.

@yield [nil, DBF::Record]

# File lib/dbf/table.rb, line 119
def each
  record_count.times { |i| yield record(i) }
end
filename() click to toggle source

@return [String]

# File lib/dbf/table.rb, line 124
def filename
  return unless @data.respond_to?(:path)

  File.basename(@data.path)
end
find(command, options = {}, &block) click to toggle source

Find records using a simple ActiveRecord-like syntax.

Examples:

table = DBF::Table.new 'mydata.dbf'

# Find record number 5
table.find(5)

# Find all records for Keith Morrison
table.find :all, first_name: "Keith", last_name: "Morrison"

# Find first record
table.find :first, first_name: "Keith"

The command may be a record index, :all, or :first. options is optional and, if specified, should be a hash where the keys correspond to column names in the database. The values will be matched exactly with the value in the database. If you specify more than one key, all values must match in order for the record to be returned. The equivalent SQL would be “WHERE key1 = 'value1' AND key2 = 'value2'”.

@param command [Integer, Symbol] command @param options [optional, Hash] options Hash of search parameters @yield [optional, DBF::Record, NilClass]

# File lib/dbf/table.rb, line 155
def find(command, options = {}, &block)
  case command
  when Integer
    record(command)
  when Array
    command.map { |i| record(i) }
  when :all
    find_all(options, &block)
  when :first
    find_first(options)
  end
end
has_memo_file?() click to toggle source

@return [TrueClass, FalseClass]

# File lib/dbf/table.rb, line 169
def has_memo_file?
  !!@memo
end
name() click to toggle source

@return [String]

# File lib/dbf/table.rb, line 174
def name
  @name ||= filename && File.basename(filename, '.*')
end
record(index) click to toggle source

Retrieve a record by index number. The record will be nil if it has been deleted, but not yet pruned from the database.

@param [Integer] index @return [DBF::Record, NilClass]

# File lib/dbf/table.rb, line 184
def record(index)
  raise DBF::NoColumnsDefined, 'The DBF file has no columns defined' if columns.empty?

  seek_to_record(index)
  return nil if deleted_record?

  record_data = @data.read(record_length)
  DBF::Record.new(record_data, columns, version, @memo)
end
Also aliased as: row
row(index)
Alias for: record
to_csv(path = nil) click to toggle source

Dumps all records to a CSV file. If no filename is given then CSV is output to STDOUT.

@param [optional String] path Defaults to STDOUT

# File lib/dbf/table.rb, line 200
def to_csv(path = nil)
  out_io = path ? File.open(path, 'w') : $stdout
  csv = CSV.new(out_io, force_quotes: true)
  csv << column_names
  each { |record| csv << record.to_a }
end
version_description() click to toggle source

Human readable version description

@return [String]

# File lib/dbf/table.rb, line 210
def version_description
  VERSIONS[version]
end

Private Instance Methods

header_size() click to toggle source
# File lib/dbf/table.rb, line 236
def header_size
  case version
  when '02'
    DBASE2_HEADER_SIZE
  when '8c'
    DBASE7_HEADER_SIZE
  else 
    DBASE3_HEADER_SIZE
  end
end