Class: DBF::Table

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Find, Schema, Enumerable
Defined in:
lib/dbf/table.rb

Overview

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

Direct Known Subclasses

Database::Table

Constant Summary

Constants included from Schema

Schema::FORMATS, Schema::OTHER_DATA_TYPES, Schema::STRING_DATA_FORMATS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Find

#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

Parameters:

  • data (String, StringIO)

    data Path to the dbf file or a StringIO object

  • memo (optional String, StringIO) (defaults to: nil)

    memo Path to the memo file or a StringIO object

  • encoding (optional String, Encoding) (defaults to: nil)

    encoding Name of the encoding or an Encoding object

Yields:

  • (_self)

Yield Parameters:

  • _self (DBF::Table)

    the object that the method was called on



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

#encodingObject (readonly)

Returns the value of attribute encoding.



18
19
20
# File 'lib/dbf/table.rb', line 18

def encoding
  @encoding
end

Instance Method Details

#closeTrueClass, FalseClass

Closes the table and memo file

Returns:

  • (TrueClass, FalseClass)


58
59
60
61
# File 'lib/dbf/table.rb', line 58

def close
  @data.close
  @memo&.close
end

#closed?TrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


64
65
66
# File 'lib/dbf/table.rb', line 64

def closed?
  @data.closed? && (!@memo || @memo.closed?)
end

#column_namesString

Column names

Returns:

  • (String)


71
72
73
# File 'lib/dbf/table.rb', line 71

def column_names
  @column_names ||= columns.map(&:name)
end

#column_offsetsArray<Integer>

Cumulative byte offsets for each column within a record

Returns:

  • (Array<Integer>)


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

#columnsArray

All columns

Returns:

  • (Array)


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.

Yields:



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

Parameters:

  • string (String)

Returns:

  • (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

#filenameString

Returns:

  • (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

Returns:

  • (TrueClass, FalseClass)


113
114
115
# File 'lib/dbf/table.rb', line 113

def has_memo_file?
  !!@memo
end

#header_encodingEncoding

Encoding specified in the file header

Returns:

  • (Encoding)


168
169
170
# File 'lib/dbf/table.rb', line 168

def header_encoding
  header.encoding
end

#nameString

Returns:

  • (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.

Parameters:

  • index (Integer)

Returns:

Raises:



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_contextObject



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.

Parameters:

  • path (optional String) (defaults to: nil)

    Defaults 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_descriptionString

Human readable version description

Returns:

  • (String)


153
154
155
# File 'lib/dbf/table.rb', line 153

def version_description
  version_config.version_description
end