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.
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
Opens a DBF::Table Examples: # working with a file stored on the filesystem table = DBF::Table.new 'data.dbf'.
- #name ⇒ String
-
#record(index) ⇒ DBF::Record, NilClass
(also: #row)
Retrieve a record by index number.
- #record_context ⇒ Object
-
#to_csv(path = 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
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
46 47 48 49 50 51 52 53 |
# File 'lib/dbf/table.rb', line 46 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.
18 19 20 |
# File 'lib/dbf/table.rb', line 18 def encoding @encoding end |
Instance Method Details
#close ⇒ TrueClass, FalseClass
Closes the table and memo file
58 59 60 61 |
# File 'lib/dbf/table.rb', line 58 def close @data.close @memo&.close end |
#closed? ⇒ TrueClass, FalseClass
64 65 66 |
# File 'lib/dbf/table.rb', line 64 def closed? @data.closed? && (!@memo || @memo.closed?) end |
#column_names ⇒ String
Column names
71 72 73 |
# File 'lib/dbf/table.rb', line 71 def column_names @column_names ||= columns.map(&:name) end |
#column_offsets ⇒ Array<Integer>
Cumulative byte offsets for each column within a record
78 79 80 81 82 83 |
# File 'lib/dbf/table.rb', line 78 def column_offsets @column_offsets ||= begin sum = 0 columns.map { |col| sum.tap { sum += col.length } } end end |
#columns ⇒ Array
All columns
92 93 94 |
# File 'lib/dbf/table.rb', line 92 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.
100 101 102 103 104 105 |
# File 'lib/dbf/table.rb', line 100 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
161 162 163 |
# File 'lib/dbf/table.rb', line 161 def encode_string(string) # :nodoc: string.force_encoding(@encoding).encode(Encoding.default_external, undef: :replace, invalid: :replace) end |
#filename ⇒ String
108 109 110 |
# File 'lib/dbf/table.rb', line 108 def filename File.basename(@data.path) if @data.is_a?(File) end |
#has_memo_file? ⇒ TrueClass, FalseClass
113 114 115 |
# File 'lib/dbf/table.rb', line 113 def has_memo_file? !!@memo end |
#header_encoding ⇒ Encoding
Encoding specified in the file header
168 169 170 |
# File 'lib/dbf/table.rb', line 168 def header_encoding header.encoding end |
#name ⇒ String
118 119 120 |
# File 'lib/dbf/table.rb', line 118 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.
128 129 130 131 132 133 134 135 136 |
# File 'lib/dbf/table.rb', line 128 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, record_context) end |
#record_context ⇒ Object
85 86 87 |
# File 'lib/dbf/table.rb', line 85 def record_context @record_context ||= RecordContext.new(columns:, version:, memo: @memo, column_offsets:) end |
#to_csv(path = nil) ⇒ Object
Dumps all records to a CSV file. If no filename is given then CSV is output to STDOUT.
144 145 146 147 148 |
# File 'lib/dbf/table.rb', line 144 def to_csv(path = nil) csv = CSV.new(path ? File.open(path, 'w') : $stdout, force_quotes: true) csv << column_names each { |record| csv << record.to_a } end |
#version_description ⇒ String
Human readable version description
153 154 155 |
# File 'lib/dbf/table.rb', line 153 def version_description version_config.version_description end |