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
Public Class Methods
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 || Encoding.default_external @memo = open_memo(data, memo) yield self if block_given? end
Public Instance Methods
Closes the table and memo file
@return [TrueClass, FalseClass]
# File lib/dbf/table.rb, line 87 def close @data.close @memo&.close end
@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
@return [String]
# File lib/dbf/table.rb, line 104 def column_names @column_names ||= columns.map(&:name) end
All columns
@return [Array]
# File lib/dbf/table.rb, line 111 def columns @columns ||= build_columns end
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
@return [String]
# File lib/dbf/table.rb, line 124 def filename return unless @data.respond_to?(:path) File.basename(@data.path) end
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
@return [TrueClass, FalseClass]
# File lib/dbf/table.rb, line 169 def has_memo_file? !!@memo end
Encoding specified in the file header
@return [Encoding]
# File lib/dbf/table.rb, line 225 def header_encoding header.encoding end
@return [String]
# File lib/dbf/table.rb, line 174 def name @name ||= filename && File.basename(filename, '.*') end
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
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
Human readable version description
@return [String]
# File lib/dbf/table.rb, line 210 def version_description VERSIONS[version] end
Private Instance Methods
# File lib/dbf/table.rb, line 251 def header_size case version when '02' DBASE2_HEADER_SIZE when '04', '8c' DBASE7_HEADER_SIZE else DBASE3_HEADER_SIZE end end