Class: RADT::Table

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

Overview

RADT::Table is the primary interface to a single RADT 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 RADT:Table Example: table = RADT::Table.new ‘data.adt’

Parameters:

  • path (String)

    Path to the adt file



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

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

Instance Attribute Details

#column_countObject (readonly)

The total number of columns



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

def column_count
  @column_count
end

#columnsObject (readonly)

An array of DBF::Column



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

def columns
  @columns
end

#dataObject (readonly)

RADT file handle



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

def data
  @data
end

#optionsObject (readonly)

The options hash used to initialize the table



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

def options
  @options
end

#record_countObject (readonly)

Total number of records



11
12
13
# File 'lib/radt/table.rb', line 11

def record_count
  @record_count
end

Instance Method Details

#closeObject

Closes the table



24
25
26
# File 'lib/radt/table.rb', line 24

def close
  @data.close
end

#column(column_name) ⇒ RADT::Column

Retrieve a Column by name

Parameters:

  • column_name (String, Symbol)

Returns:



40
41
42
# File 'lib/radt/table.rb', line 40

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

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



48
49
50
51
52
53
# File 'lib/radt/table.rb', line 48

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

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

Find records using a simple ActiveRecord-like syntax.

Examples: table = RADT::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:



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

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) ⇒ RADT::Record Also known as: row

Retrieve a record by index number

Parameters:

  • index (Fixnum)

Returns:



59
60
61
62
# File 'lib/radt/table.rb', line 59

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

#reload!Object

Reloads the database



29
30
31
32
33
# File 'lib/radt/table.rb', line 29

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)


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

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



104
105
106
107
108
# File 'lib/radt/table.rb', line 104

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



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

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