Class: Kladr::DBF::Table
- Inherits:
-
Object
- Object
- Kladr::DBF::Table
- Defined in:
- lib/dbf/dbf/table.rb
Instance Attribute Summary collapse
-
#column_count ⇒ Object
readonly
The total number of columns (columns).
-
#columns ⇒ Object
readonly
An array of DBF::Column records.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#last_updated ⇒ Object
readonly
Last updated datetime.
-
#memo ⇒ Object
readonly
Returns the value of attribute memo.
-
#memo_block_size ⇒ Object
readonly
The block size for memo records.
-
#memo_file_format ⇒ Object
readonly
Either :fpt or :dpt.
-
#options ⇒ Object
readonly
The options that were used when initializing DBF::Table.
-
#version ⇒ Object
readonly
Internal dBase version number.
Instance Method Summary collapse
-
#column(column_name) ⇒ Object
Returns an instance of DBF::Column for column_name.
-
#find(command, options = {}) ⇒ Object
Find records using a simple ActiveRecord-like syntax.
-
#has_memo_file? ⇒ Boolean
Returns true if there is a corresponding memo file.
-
#initialize(filename, options = {}) ⇒ Table
constructor
Initialize a new DBF::Reader.
-
#record(index) ⇒ Object
(also: #row)
Returns a DBF::Record (or nil if the record has been marked for deletion) for the record at
index
. -
#record_count ⇒ Object
The total number of active records.
-
#records ⇒ Object
(also: #rows)
An array of all the records contained in the database file.
-
#reload! ⇒ Object
Reloads the database and memo files.
-
#schema(path = nil) ⇒ Object
Returns a database schema in the portable ActiveRecord::Schema format.
-
#version_description ⇒ Object
Returns a description of the current database file.
Constructor Details
#initialize(filename, options = {}) ⇒ Table
Initialize a new DBF::Reader. Example:
reader = DBF::Reader.new 'data.dbf'
32 33 34 35 36 37 38 39 40 |
# File 'lib/dbf/dbf/table.rb', line 32 def initialize(filename, = {}) @options = {:in_memory => true, :accessors => true}.merge() @in_memory = @options[:in_memory] @accessors = @options[:accessors] @data = File.open(filename, 'rb') @memo = open_memo(filename) reload! end |
Instance Attribute Details
#column_count ⇒ Object (readonly)
The total number of columns (columns)
6 7 8 |
# File 'lib/dbf/dbf/table.rb', line 6 def column_count @column_count end |
#columns ⇒ Object (readonly)
An array of DBF::Column records
9 10 11 |
# File 'lib/dbf/dbf/table.rb', line 9 def columns @columns end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
26 27 28 |
# File 'lib/dbf/dbf/table.rb', line 26 def data @data end |
#last_updated ⇒ Object (readonly)
Last updated datetime
15 16 17 |
# File 'lib/dbf/dbf/table.rb', line 15 def last_updated @last_updated end |
#memo ⇒ Object (readonly)
Returns the value of attribute memo.
27 28 29 |
# File 'lib/dbf/dbf/table.rb', line 27 def memo @memo end |
#memo_block_size ⇒ Object (readonly)
The block size for memo records
21 22 23 |
# File 'lib/dbf/dbf/table.rb', line 21 def memo_block_size @memo_block_size end |
#memo_file_format ⇒ Object (readonly)
Either :fpt or :dpt
18 19 20 |
# File 'lib/dbf/dbf/table.rb', line 18 def memo_file_format @memo_file_format end |
#options ⇒ Object (readonly)
The options that were used when initializing DBF::Table. This is a Hash.
24 25 26 |
# File 'lib/dbf/dbf/table.rb', line 24 def @options end |
#version ⇒ Object (readonly)
Internal dBase version number
12 13 14 |
# File 'lib/dbf/dbf/table.rb', line 12 def version @version end |
Instance Method Details
#column(column_name) ⇒ Object
Returns an instance of DBF::Column for column_name. column_name can be a symbol or a string.
63 64 65 |
# File 'lib/dbf/dbf/table.rb', line 63 def column(column_name) @columns.detect {|f| f.name == column_name.to_s} end |
#find(command, options = {}) ⇒ Object
Find records using a simple ActiveRecord-like syntax.
Examples:
reader = DBF::Reader.new 'mydata.dbf'
# Find record number 5
reader.find(5)
# Find all records for Keith Morrison
reader.find :all, :first_name => "Keith", :last_name => "Morrison"
# Find first record
reader.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’”.
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/dbf/dbf/table.rb', line 108 def find(command, = {}) results = .empty? ? records : records.select {|record| all_values_match?(record, )} case command when Fixnum record(command) when :all results when :first results.first end end |
#has_memo_file? ⇒ Boolean
Returns true if there is a corresponding memo file
52 53 54 |
# File 'lib/dbf/dbf/table.rb', line 52 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
.
80 81 82 83 84 85 86 |
# File 'lib/dbf/dbf/table.rb', line 80 def record(index) if [:in_memory] records[index] else get_record_from_file(index) end end |
#record_count ⇒ Object
The total number of active records.
57 58 59 |
# File 'lib/dbf/dbf/table.rb', line 57 def record_count @db_index.size end |
#records ⇒ Object 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).
69 70 71 72 73 74 75 |
# File 'lib/dbf/dbf/table.rb', line 69 def records if [:in_memory] @records ||= get_all_records_from_file else get_all_records_from_file end end |
#reload! ⇒ Object
Reloads the database and memo files
43 44 45 46 47 48 49 |
# File 'lib/dbf/dbf/table.rb', line 43 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
147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/dbf/dbf/table.rb', line 147 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_description ⇒ Object
Returns a description of the current database file.
124 125 126 |
# File 'lib/dbf/dbf/table.rb', line 124 def version_description VERSION_DESCRIPTIONS[version] end |