Class: DBF::Table

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/dbf/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, options = {}) ⇒ Table

Initializes a new DBF::Table Example:

table = DBF::Table.new 'data.dbf'


19
20
21
22
23
24
# File 'lib/dbf/table.rb', line 19

def initialize(filename, options = {})
  @data = File.open(filename, 'rb')
  @memo = open_memo(filename)
  @options = options
  reload!
end

Instance Attribute Details

#column_countObject (readonly)

The total number of columns (columns)



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

def column_count
  @column_count
end

#columnsObject (readonly)

An array of DBF::Column



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

def columns
  @columns
end

#dataObject (readonly)

DBF file handle



13
14
15
# File 'lib/dbf/table.rb', line 13

def data
  @data
end

#last_updatedObject (readonly)

Last updated datetime



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

def last_updated
  @last_updated
end

#memoObject (readonly)

Memo file handle



14
15
16
# File 'lib/dbf/table.rb', line 14

def memo
  @memo
end

#memo_block_sizeObject (readonly)

The block size for memo records



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

def memo_block_size
  @memo_block_size
end

#memo_file_formatObject (readonly)

:fpt or :dpt



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

def memo_file_format
  @memo_file_format
end

#optionsObject (readonly)

The options hash that was used to initialize the table



12
13
14
# File 'lib/dbf/table.rb', line 12

def options
  @options
end

#versionObject (readonly)

Internal dBase version number



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

def version
  @version
end

Instance Method Details

#column(column_name) ⇒ Object

Returns an instance of DBF::Column for column_name. The column_name can be a specified as either a symbol or string.



47
48
49
# File 'lib/dbf/table.rb', line 47

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

#eachObject



59
60
61
62
63
64
65
66
# File 'lib/dbf/table.rb', line 59

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

#find(command, options = {}) ⇒ Object

Find records using a simple ActiveRecord-like syntax.

Examples:

table = DBF::Table.new 'mydata.dbf'

# Find record number 5
table.find(5)

# Find all records for Keith Morrison
table.find :all, :first_name => "Keith", :last_name => "Morrison"

# Find first record
table.find :first, :first_name => "Keith"

The command can be an id, :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’”.



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dbf/table.rb', line 98

def find(command, options = {})
  results = options.empty? ? records : records.select {|record| all_values_match?(record, options)}
  
  case command
  when Fixnum
    record(command)
  when :all
    results
  when :first
    results.first
  end
end

#get_record_from_file(index) ⇒ Object

Returns the record at index by seeking to the record in the physical database file. See the documentation for the records method for information on how these two methods differ.



155
156
157
158
# File 'lib/dbf/table.rb', line 155

def get_record_from_file(index)
  seek_to_record(@db_index[index])
  Record.new(self)
end

#has_memo_file?Boolean

Returns true if there is a corresponding memo file

Returns:

  • (Boolean)


36
37
38
# File 'lib/dbf/table.rb', line 36

def has_memo_file?
  @memo ? true : false
end

#record(index) ⇒ Object Also known as: row

Returns a DBF::Record (or nil if the record has been marked for deletion) for the record at index.



74
75
76
# File 'lib/dbf/table.rb', line 74

def record(index)
  records[index]
end

#record_countObject

The total number of active records.



41
42
43
# File 'lib/dbf/table.rb', line 41

def record_count
  @db_index.size
end

#recordsObject Also known as: rows

An array of all the records contained in the database file. Each record is an instance of DBF::Record (or nil if the record is marked for deletion).



53
54
55
# File 'lib/dbf/table.rb', line 53

def records
  self.to_a
end

#reload!Object

Reloads the database and memo files



27
28
29
30
31
32
33
# File 'lib/dbf/table.rb', line 27

def reload!
  @records = nil
  get_header_info
  get_memo_header_info if @memo
  get_column_descriptors
  build_db_index
end

#schema(path = nil) ⇒ Object

Returns a database schema in the portable ActiveRecord::Schema format.

xBase data types are converted to generic types as follows:

  • Number columns are converted to :integer if there are no decimals, otherwise they 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


137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/dbf/table.rb', line 137

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)}
  else
    s
  end
end

#version_descriptionObject

Returns a description of the current database file.



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

def version_description
  VERSION_DESCRIPTIONS[version]
end