dbf 0.5.0 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'hoe'
2
2
  require 'spec/rake/spectask'
3
3
 
4
4
  PKG_NAME = "dbf"
5
- PKG_VERSION = "0.5.0"
5
+ PKG_VERSION = "0.5.1"
6
6
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
7
7
 
8
8
  Hoe.new PKG_NAME, PKG_VERSION do |p|
data/lib/dbf/record.rb CHANGED
@@ -27,40 +27,45 @@ module DBF
27
27
  self
28
28
  end
29
29
 
30
- def unpack_field(field)
31
- @data_file.read(field.length).unpack("a#{field.length}")
32
- end
30
+ private
33
31
 
34
- def unpack_string(field)
35
- unpack_field(field).to_s
36
- end
32
+ def unpack_field(field)
33
+ @data_file.read(field.length).unpack("a#{field.length}")
34
+ end
35
+
36
+ def unpack_string(field)
37
+ unpack_field(field).to_s
38
+ end
37
39
 
38
- def read_memo(start_block)
39
- return nil if start_block == 0
40
- @memo_file.seek(start_block * @reader.memo_block_size)
41
- if @reader.memo_file_format == :fpt
42
- memo_type, memo_size, memo_string = @memo_file.read(@reader.memo_block_size).unpack("NNa56")
40
+ def read_memo(start_block)
41
+ return nil if start_block == 0
42
+ @memo_file.seek(start_block * @reader.memo_block_size)
43
+ if @reader.memo_file_format == :fpt
44
+ memo_type, memo_size, memo_string = @memo_file.read(@reader.memo_block_size).unpack("NNa56")
43
45
 
44
- memo_block_content_size = @reader.memo_block_size - FPT_BLOCK_HEADER_SIZE
45
- if memo_size > memo_block_content_size
46
- memo_string << @memo_file.read(memo_size - @reader.memo_block_size + FPT_BLOCK_HEADER_SIZE)
47
- elsif memo_size > 0 and memo_size < memo_block_content_size
48
- memo_string = memo_string[0, memo_size]
49
- end
50
- else
51
- case @reader.version
52
- when "83" # dbase iii
53
- memo_string = ""
54
- loop do
55
- memo_string << block = @memo_file.read(512)
56
- break if block.strip.size < 512
46
+ # skip the memo if it isn't texst
47
+ return nil unless memo_type == 1
48
+
49
+ memo_block_content_size = @reader.memo_block_size - FPT_BLOCK_HEADER_SIZE
50
+ if memo_size > memo_block_content_size
51
+ memo_string << @memo_file.read(memo_size - @reader.memo_block_size + FPT_BLOCK_HEADER_SIZE)
52
+ elsif memo_size > 0 and memo_size < memo_block_content_size
53
+ memo_string = memo_string[0, memo_size]
54
+ end
55
+ else
56
+ case @reader.version
57
+ when "83" # dbase iii
58
+ memo_string = ""
59
+ loop do
60
+ memo_string << block = @memo_file.read(512)
61
+ break if block.strip.size < 512
62
+ end
63
+ when "8b" # dbase iv
64
+ memo_type, memo_size = @memo_file.read(8).unpack("LL")
65
+ memo_string = @memo_file.read(memo_size)
57
66
  end
58
- when "8b" # dbase iv
59
- memo_type, memo_size = @memo_file.read(8).unpack("LL")
60
- memo_string = @memo_file.read(memo_size)
61
67
  end
68
+ memo_string
62
69
  end
63
- memo_string
64
70
  end
65
- end
66
71
  end
data/spec/field_spec.rb CHANGED
@@ -6,19 +6,19 @@ describe DBF::Field, "when initialized" do
6
6
  @field = DBF::Field.new "FieldName", "N", 1, 0
7
7
  end
8
8
 
9
- it "should set the 'name' accessor" do
9
+ it "should set the #name accessor" do
10
10
  @field.name.should == "FieldName"
11
11
  end
12
12
 
13
- it "should set the 'type' accessor" do
13
+ it "should set the #type accessor" do
14
14
  @field.type.should == "N"
15
15
  end
16
16
 
17
- it "should set the 'length' accessor" do
17
+ it "should set the #length accessor" do
18
18
  @field.length.should == 1
19
19
  end
20
20
 
21
- it "should set the 'decimal' accessor" do
21
+ it "should set the #decimal accessor" do
22
22
  @field.decimal.should == 0
23
23
  end
24
24
 
data/spec/reader_spec.rb CHANGED
@@ -140,3 +140,32 @@ describe DBF::Reader, "find(:first)" do
140
140
  end
141
141
  end
142
142
 
143
+ describe DBF::Reader do
144
+
145
+ before(:each) do
146
+ @reader = DBF::Reader.new File.dirname(__FILE__) + '/../test/databases/dbase_iii_memo.dbf'
147
+ end
148
+
149
+ it "should reload all data when sent #reload!" do
150
+ @reader.records
151
+ @reader.instance_eval("@records").should be_kind_of(Array)
152
+ @reader.reload!
153
+ @reader.instance_eval("@records").should be_nil
154
+ end
155
+
156
+ it "should return a DBF::Field object when sent #field with a valid field_name given as a string or symbol" do
157
+ @reader.field("IMAGE").should be_kind_of(DBF::Field)
158
+ @reader.field(:IMAGE).should be_kind_of(DBF::Field)
159
+ end
160
+
161
+ it "should return nil when sent #field with an invalid field_name given as a string or symbol" do
162
+ @reader.field("NOTANIMAGE").should be_nil
163
+ @reader.field(:NOTANIMAGE).should be_nil
164
+ end
165
+
166
+ it "should return a text description of the database type when sent #version_description" do
167
+ @reader.version_description.should == "dBase III with memo file"
168
+ end
169
+
170
+ end
171
+
data/spec/record_spec.rb CHANGED
@@ -2,8 +2,31 @@ require File.dirname(__FILE__) + "/spec_helper"
2
2
 
3
3
  describe DBF::Record, "when initialized" do
4
4
 
5
- it "should convert number fields with 0 decimals to integers" do
6
-
5
+ before(:each) do
6
+ @reader = DBF::Reader.new File.dirname(__FILE__) + '/../test/databases/dbase_iii_memo.dbf'
7
+ @record = @reader.record(5)
8
+ end
9
+
10
+ it "should typecast number fields with decimals == 0 to Integer" do
11
+ @reader.field("ID").type == "N"
12
+ @reader.field("ID").decimal.should == 0
13
+ @record["ID"].should be_kind_of(Integer)
14
+ end
15
+
16
+ it "should typecast number fields with decimals > 0 to Float" do
17
+ @reader.field("ID").type == "N"
18
+ @reader.field("COST").decimal.should == 2
19
+ @record["COST"].should be_kind_of(Float)
20
+ end
21
+
22
+ it "should typecast memo fields to String" do
23
+ @reader.field("DESC").type == "M"
24
+ @record["DESC"].should be_kind_of(String)
25
+ end
26
+
27
+ it "should typecast logical fields to True or False" do
28
+ @reader.field("TAXABLE").type == "L"
29
+ @record["TAXABLE"].should be_kind_of(FalseClass)
7
30
  end
8
31
 
9
32
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.4
3
3
  specification_version: 1
4
4
  name: dbf
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.0
7
- date: 2007-05-26 00:00:00 -07:00
6
+ version: 0.5.1
7
+ date: 2007-06-05 00:00:00 -07:00
8
8
  summary: A small fast library for reading dBase, xBase, Clipper and FoxPro database files.
9
9
  require_paths:
10
10
  - lib