Class: ADT::Table

Inherits:
Object show all
Defined in:
lib/adt/table.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Table

Opens a ADT:Table Example: table = ADT::Table.new ‘data.adt’

Parameters:

  • path (String)

    Path to the adt file



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

def initialize(path)
  @data = File.open(path, 'rb')
  reload!
end

Instance Attribute Details

#column_countObject (readonly)

The total number of columns



6
7
8
# File 'lib/adt/table.rb', line 6

def column_count
  @column_count
end

#columnsObject (readonly)

An array of DBF::Column



7
8
9
# File 'lib/adt/table.rb', line 7

def columns
  @columns
end

#dataObject (readonly)

ADT file handle



9
10
11
# File 'lib/adt/table.rb', line 9

def data
  @data
end

#optionsObject (readonly)

The options hash used to initialize the table



8
9
10
# File 'lib/adt/table.rb', line 8

def options
  @options
end

#record_countObject (readonly)

Total number of records



10
11
12
# File 'lib/adt/table.rb', line 10

def record_count
  @record_count
end

Instance Method Details

#closeObject

Closes the table



23
24
25
# File 'lib/adt/table.rb', line 23

def close
  @data.close
end

#column(column_name) ⇒ ADT::Column

Retrieve a Column by name

Parameters:

  • column_name (String, Symbol)

Returns:



39
40
41
# File 'lib/adt/table.rb', line 39

def column(column_name)
  @columns.detect {|f| f.name == column_name.to_s}
end

#each {|nil, ADT::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:



47
48
49
50
51
52
# File 'lib/adt/table.rb', line 47

def each
  0.upto(@record_count - 1) do |n|
    seek_to_record(n)
    yield ADT::Record.new(self)
  end
end

#find(command, options = {}) {|optional, ADT::Record| ... } ⇒ Object

Find records using a simple ActiveRecord-like syntax.

Examples: table = ADT::Table.new ‘mydata.adt’

# Find record number 5 table.find(5)

# Find all records for Chase Gray table.find :all, :first_name => “Chase”, :last_name => “Gray”

# Find first record table.find :first, :first_name => “Chase”

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’”.

Parameters:

  • command (Fixnum, Symbol)
  • options (optional, Hash) (defaults to: {})

    Hash of search parameters

Yields:



146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/adt/table.rb', line 146

def find(command, options = {}, &block)
  case command
  when Fixnum
    record(command)
  when Array
    command.map {|i| record(i)}
  when :all
    find_all(options, &block)
  when :first
    find_first(options)
  end
end

#record(index) ⇒ ADT::Record Also known as: row

Retrieve a record by index number

Parameters:

  • index (Fixnum)

Returns:



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

def record(index)
  seek_to_record(index)
  ADT::Record.new(self)
end

#reload!Object

Reloads the database



28
29
30
31
32
# File 'lib/adt/table.rb', line 28

def reload!
  @records = nil
  get_header_info
  get_column_descriptors
end

#schema(path = nil) ⇒ String

Generate an ActiveRecord::Schema

xBase data types are converted to generic types as follows:

  • Number columns with no decimals are converted to :integer

  • Number columns with decimals are converted to :float

  • Date columns are converted to :datetime

  • Logical columns are converted to :boolean

  • Memo columns are converted to :text

  • Character columns are converted to :string and the :limit option is set

to the length of the character column

Example: create_table “mydata” do |t| t.column :name, :string, :limit => 30 t.column :last_update, :datetime t.column :is_active, :boolean t.column :age, :integer t.column :notes, :text end

Parameters:

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

Returns:

  • (String)


88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/adt/table.rb', line 88

def schema(path = nil)
  s = "ActiveRecord::Schema.define do\n"
  s << " create_table \"#{File.basename(@data.path, ".*")}\" do |t|\n"
  columns.each do |column|
    s << " t.column #{column.schema_definition}"
  end
  s << " end\nend"
  
  if path
    File.open(path, 'w') {|f| f.puts(s)}
  end
    
  s
end

#to_aObject



103
104
105
106
107
# File 'lib/adt/table.rb', line 103

def to_a
  records = []
  each {|record| records << record if record}
  records
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 basename of adt file



113
114
115
116
117
118
119
120
# File 'lib/adt/table.rb', line 113

def to_csv(path = nil)
  path = File.basename(@data.path, '.adt') + '.csv' if path.nil?
  FCSV.open(path, 'w', :force_quotes => true) do |csv|
    each do |record|
      csv << record.to_a
    end
  end
end