Skip to content
This repository has been archived by the owner on Nov 12, 2022. It is now read-only.

Commit

Permalink
experimental support for visual foxpro double (b) data type
Browse files Browse the repository at this point in the history
  • Loading branch information
infused committed Oct 28, 2011
1 parent 92e1815 commit a8d2a27
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions lib/dbf.rb
Expand Up @@ -10,5 +10,6 @@
require 'dbf/attributes'
require 'dbf/record'
require 'dbf/column'
require 'dbf/foxpro_column'
require 'dbf/memo'
require 'dbf/table'
4 changes: 4 additions & 0 deletions lib/dbf/column.rb
Expand Up @@ -32,6 +32,7 @@ def type_cast(value)
when 'D' then decode_date(value)
when 'T' then decode_datetime(value)
when 'L' then boolean(value)
when 'B' then unpack_binary(value)
else encode_string(value.to_s).strip
end
end
Expand Down Expand Up @@ -73,6 +74,9 @@ def unpack_number(value) #nodoc
def unpack_unsigned_long(value) #nodoc
value.unpack('V')[0]
end

def unpack_binary(value) #nodoc
end

def boolean(value) #nodoc
value.strip =~ /^(y|t)$/i ? true : false
Expand Down
7 changes: 7 additions & 0 deletions lib/dbf/foxpro_column.rb
@@ -0,0 +1,7 @@
module DBF
class FoxproColumn < Column
def unpack_binary(value) #nodoc
value.unpack('d')[0]
end
end
end
10 changes: 9 additions & 1 deletion lib/dbf/table.rb
Expand Up @@ -21,6 +21,10 @@ class Table
"f5" => "FoxPro with memo file",
"fb" => "FoxPro without memo file"
}

FOXPRO_VERSIONS = VERSION_DESCRIPTIONS.map do |version, description|
version if description =~ /FoxPro/
end.compact

attr_reader :version # Internal dBase version number
attr_reader :record_count # Total number of records
Expand Down Expand Up @@ -164,14 +168,18 @@ def find(command, options = {}, &block)

# Retrieves column information from the database
def columns
column_class = FOXPRO_VERSIONS.include?(version) ? FoxproColumn : Column

@columns ||= begin
column_count = (@header_length - DBF_HEADER_SIZE + 1) / DBF_HEADER_SIZE

@data.seek(DBF_HEADER_SIZE)
columns = []
column_count.times do
name, type, length, decimal = @data.read(32).unpack('a10 x a x4 C2')
columns << Column.new(name.strip, type, length, decimal, @encoding) if length > 0
if length > 0
columns << column_class.new(name.strip, type, length, decimal, @encoding)
end
end
columns
end
Expand Down
12 changes: 7 additions & 5 deletions spec/dbf/file_formats_spec.rb
Expand Up @@ -16,10 +16,6 @@
@table.count.should == @table.record_count
end

specify "columns should be instances of DBF::Column" do
@table.columns.all? {|column| column.should be_an_instance_of(DBF::Column)}
end

specify "column names should not be blank" do
@table.columns.all? {|column| column.name.should_not be_empty}
end
Expand Down Expand Up @@ -54,7 +50,12 @@
record.send(column_name).should == record.send(Util.underscore(column_name))
end
end

end

shared_examples_for 'Foxpro DBF' do
specify "columns should be instances of DBF::FoxproColumn" do
@table.columns.all? {|column| column.should be_an_instance_of(DBF::FoxproColumn)}
end
end

describe DBF, "of type 03 (dBase III without memo file)" do
Expand Down Expand Up @@ -163,6 +164,7 @@
end

it_should_behave_like "DBF"
it_should_behave_like "Foxpro DBF"

it "should report the correct version number" do
@table.version.should == "f5"
Expand Down
4 changes: 4 additions & 0 deletions spec/dbf/table_spec.rb
@@ -1,6 +1,10 @@
require "spec_helper"

describe DBF::Table do
# specify do
# DBF::Table::FOXPRO_VERSIONS.should == %w(30 31 f5 fb)
# end

context "when closed" do
before do
@table = DBF::Table.new "#{DB_PATH}/dbase_83.dbf"
Expand Down

0 comments on commit a8d2a27

Please sign in to comment.