Class: DBF::Table
Overview
DBF::Table is the primary interface to a single DBF file and provides methods for enumerating and searching the records.
Direct Known Subclasses
Constant Summary
Constants included from Schema
Schema::FORMATS, Schema::OTHER_DATA_TYPES, Schema::STRING_DATA_FORMATS
Instance Attribute Summary collapse
-
#encoding ⇒ Object
readonly
Returns the value of attribute encoding.
Class Method Summary collapse
-
.open(data, memo = nil, encoding = nil, name: nil) ⇒ Object
Opens a DBF::Table Examples: # working with a file stored on the filesystem table = DBF::Table.new 'data.dbf'.
Instance Method Summary collapse
-
#close ⇒ TrueClass, FalseClass
Closes the table and memo file.
- #closed? ⇒ TrueClass, FalseClass
-
#column_names ⇒ String
Column names.
-
#column_offsets ⇒ Array<Integer>
Cumulative byte offsets for each column within a record.
-
#columns ⇒ Array
All columns.
-
#each {|nil, DBF::Record| ... } ⇒ Object
Calls block once for each record in the table.
-
#encode_string(string) ⇒ String
Encode string.
- #filename ⇒ String
- #has_memo_file? ⇒ TrueClass, FalseClass
-
#header_encoding ⇒ Encoding
Encoding specified in the file header.
-
#initialize(data, memo = nil, encoding = nil, name: nil) {|_self| ... } ⇒ Table
constructor
A new instance of Table.
- #name ⇒ String
-
#record(index) ⇒ DBF::Record, NilClass
(also: #row)
Retrieve a record by index number.
- #record_context ⇒ Object
-
#to_csv(path_or_io = nil) ⇒ Object
Dumps all records to a CSV file.
-
#version_description ⇒ String
Human readable version description.
Methods included from Find
Methods included from Schema
#activerecord_schema, #activerecord_schema_definition, #json_schema, #number_data_type, #schema, #schema_data_type, #schema_name, #sequel_schema, #sequel_schema_definition, #string_data_format
Constructor Details
#initialize(data, memo = nil, encoding = nil, name: nil) {|_self| ... } ⇒ Table
Returns a new instance of Table.
67 68 69 70 71 72 73 74 |
# File 'lib/dbf/table.rb', line 67 def initialize(data, memo = nil, encoding = nil, name: nil) @data = FileHandler.open_data(data) @user_encoding = encoding @encoding = determine_encoding @memo = FileHandler.open_memo(data, memo, version_config.memo_class, version) @name = name yield self if block_given? end |
Instance Attribute Details
#encoding ⇒ Object (readonly)
Returns the value of attribute encoding.
16 17 18 |
# File 'lib/dbf/table.rb', line 16 def encoding @encoding end |
Class Method Details
.open(data, memo = nil, encoding = nil, name: nil) ⇒ Object
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 an open IO object
table = DBF::Table.new File.open('data.dbf', 'rb')
# 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
Opens a table like .new, but when given a block, yields the table, closes it when the block returns, and returns the block's value — the same contract as File.open.
DBF::Table.open('data.dbf') do |table|
table.each { |record| ... }
end
Takes the same arguments as .new. Without a block, equivalent to .new.
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/dbf/table.rb', line 53 def self.open(data, memo = nil, encoding = nil, name: nil) table = new(data, memo, encoding, name: name) return table unless block_given? begin yield table ensure table.close end end |
Instance Method Details
#close ⇒ TrueClass, FalseClass
Closes the table and memo file
79 80 81 82 |
# File 'lib/dbf/table.rb', line 79 def close @data.close @memo&.close end |
#closed? ⇒ TrueClass, FalseClass
85 86 87 |
# File 'lib/dbf/table.rb', line 85 def closed? @data.closed? && (!@memo || @memo.closed?) end |
#column_names ⇒ String
Column names
92 93 94 |
# File 'lib/dbf/table.rb', line 92 def column_names @column_names ||= columns.map(&:name) end |
#column_offsets ⇒ Array<Integer>
Cumulative byte offsets for each column within a record
99 100 101 102 103 104 |
# File 'lib/dbf/table.rb', line 99 def column_offsets @column_offsets ||= begin sum = 0 columns.map { |col| sum.tap { sum += col.length } } end end |
#columns ⇒ Array
All columns
113 114 115 |
# File 'lib/dbf/table.rb', line 113 def columns @columns ||= build_columns end |
#each {|nil, DBF::Record| ... } ⇒ Object
Calls block once for each record in the table. The record may be nil if the record has been marked as deleted.
121 122 123 124 125 126 |
# File 'lib/dbf/table.rb', line 121 def each(&) return enum_for(:each) unless block_given? return if columns.empty? RecordIterator.new(@data, record_context, header_length, record_length, record_count).each(&) end |
#encode_string(string) ⇒ String
Encode string
188 189 190 |
# File 'lib/dbf/table.rb', line 188 def encode_string(string) # :nodoc: Encoder.encode(string, @encoding) end |
#filename ⇒ String
129 130 131 |
# File 'lib/dbf/table.rb', line 129 def filename File.basename(@data.path) if @data.respond_to?(:path) end |
#has_memo_file? ⇒ TrueClass, FalseClass
134 135 136 |
# File 'lib/dbf/table.rb', line 134 def has_memo_file? !!@memo end |
#header_encoding ⇒ Encoding
Encoding specified in the file header
195 196 197 |
# File 'lib/dbf/table.rb', line 195 def header_encoding header.encoding end |
#name ⇒ String
139 140 141 |
# File 'lib/dbf/table.rb', line 139 def name @name ||= filename && File.basename(filename, '.*') end |
#record(index) ⇒ DBF::Record, NilClass Also known as: row
Retrieve a record by index number. The record will be nil if it has been deleted, but not yet pruned from the database.
149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/dbf/table.rb', line 149 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) # A file that ends immediately after the delete flag has no record body; # treat it as absent rather than building a Record over nil data. return nil unless record_data DBF::Record.new(record_data, record_context) end |
#record_context ⇒ Object
106 107 108 |
# File 'lib/dbf/table.rb', line 106 def record_context @record_context ||= RecordContext.new(columns:, version:, memo: @memo, column_offsets:) end |
#to_csv(path_or_io = nil) ⇒ Object
Dumps all records to a CSV file. If no filename is given then CSV is output to STDOUT.
169 170 171 172 173 174 175 |
# File 'lib/dbf/table.rb', line 169 def to_csv(path_or_io = nil) if path_or_io.is_a?(String) File.open(path_or_io, 'w') { |file| write_csv(file) } else write_csv(path_or_io || $stdout) end end |
#version_description ⇒ String
Human readable version description
180 181 182 |
# File 'lib/dbf/table.rb', line 180 def version_description version_config.version_description end |