dbf 1.0.6 → 1.0.7

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt CHANGED
@@ -1,3 +1,7 @@
1
+ == 1.0.7
2
+
3
+ * Remove support for original column names. All columns names are now downcased/underscored.
4
+
1
5
  == 1.0.6
2
6
 
3
7
  * DBF::Table now includes the Enumerable module
data/README.txt CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  DBF is a small fast library for reading dBase, xBase, Clipper and FoxPro database files. It is written completely in Ruby and has no external dependencies.
4
4
 
5
- Copyright (c) 2006-2007 Keith Morrison <keithm@infused.org, www.infused.org>
5
+ Copyright (c) 2006-2008 Keith Morrison <keithm@infused.org, www.infused.org>
6
6
 
7
7
  * Official project page: http://rubyforge.org/projects/dbf
8
8
  * API Documentation: http://dbf.rubyforge.org/docs
@@ -14,11 +14,8 @@ Copyright (c) 2006-2007 Keith Morrison <keithm@infused.org, www.infused.org>
14
14
 
15
15
  * No external dependencies
16
16
  * Fields are type cast to the appropriate Ruby types
17
- * Date/Time fields are returned as either a Time or Date object. Date
18
- will only be used if the date is out of range for Ruby's built in Time
19
- class.
20
17
  * Ability to dump the database schema in the portable ActiveRecord::Schema
21
- format.
18
+ format
22
19
 
23
20
  == Installation
24
21
 
@@ -29,7 +26,12 @@ Copyright (c) 2006-2007 Keith Morrison <keithm@infused.org, www.infused.org>
29
26
  require 'rubygems'
30
27
  require 'dbf'
31
28
 
32
- table = DBF::Table.new("old_data.dbf")
29
+ table = DBF::Table.new("widgets.dbf")
30
+
31
+ # Tables are enumerable
32
+ widget_ids = table.map { |row| row.id }
33
+ abc_names = table.select { |row| row.name =~ /^[a-cA-C] }
34
+ sorted = table.sort_by { |row| row.name }
33
35
 
34
36
  # Print the 'name' field from record number 4
35
37
  puts table.record(4).name
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 = "1.0.6"
5
+ PKG_VERSION = "1.0.7"
6
6
  PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
7
7
 
8
8
  Hoe.new PKG_NAME, PKG_VERSION do |p|
@@ -15,6 +15,7 @@ Hoe.new PKG_NAME, PKG_VERSION do |p|
15
15
  p.url = "http://github.com/infused/dm-dbf/tree/master"
16
16
  p.need_tar = true
17
17
  p.need_zip = true
18
+ p.extra_deps << ['activesupport', '>= 2.1.0']
18
19
  end
19
20
 
20
21
  task :default => :spec
data/dbf.gemspec CHANGED
@@ -1,13 +1,13 @@
1
1
  (in /Users/keithm/projects/dbf)
2
2
  Gem::Specification.new do |s|
3
3
  s.name = %q{dbf}
4
- s.version = "1.0.6"
4
+ s.version = "1.0.7"
5
5
 
6
6
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
7
7
  s.authors = ["Keith Morrison"]
8
- s.date = %q{2008-06-30}
8
+ s.date = %q{2008-09-02}
9
9
  s.default_executable = %q{dbf}
10
- s.description = %q{DBF is a small fast library for reading dBase, xBase, Clipper and FoxPro database files. It is written completely in Ruby and has no external dependencies. Copyright (c) 2006-2007 Keith Morrison <keithm@infused.org, www.infused.org> * Official project page: http://rubyforge.org/projects/dbf * API Documentation: http://dbf.rubyforge.org/docs * To report bugs: http://www.rubyforge.org/tracker/?group_id=2009 * Questions: Email keithm@infused.org and put DBF somewhere in the subject line}
10
+ s.description = %q{DBF is a small fast library for reading dBase, xBase, Clipper and FoxPro database files. It is written completely in Ruby and has no external dependencies. Copyright (c) 2006-2008 Keith Morrison <keithm@infused.org, www.infused.org> * Official project page: http://rubyforge.org/projects/dbf * API Documentation: http://dbf.rubyforge.org/docs * To report bugs: http://www.rubyforge.org/tracker/?group_id=2009 * Questions: Email keithm@infused.org and put DBF somewhere in the subject line}
11
11
  s.email = %q{keithm@infused.org}
12
12
  s.executables = ["dbf"]
13
13
  s.extra_rdoc_files = ["History.txt", "Manifest.txt", "README.txt", "spec/fixtures/dbase_83_schema.txt"]
@@ -25,11 +25,14 @@ Gem::Specification.new do |s|
25
25
  s.specification_version = 2
26
26
 
27
27
  if current_version >= 3 then
28
- s.add_runtime_dependency(%q<hoe>, [">= 1.6.0"])
28
+ s.add_runtime_dependency(%q<activesupport>, [">= 2.1.0"])
29
+ s.add_development_dependency(%q<hoe>, [">= 1.7.0"])
29
30
  else
30
- s.add_dependency(%q<hoe>, [">= 1.6.0"])
31
+ s.add_dependency(%q<activesupport>, [">= 2.1.0"])
32
+ s.add_dependency(%q<hoe>, [">= 1.7.0"])
31
33
  end
32
34
  else
33
- s.add_dependency(%q<hoe>, [">= 1.6.0"])
35
+ s.add_dependency(%q<activesupport>, [">= 2.1.0"])
36
+ s.add_dependency(%q<hoe>, [">= 1.7.0"])
34
37
  end
35
38
  end
data/lib/dbf.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'date'
2
+ require 'activesupport'
2
3
 
3
4
  require 'dbf/globals'
4
5
  require 'dbf/record'
data/lib/dbf/column.rb CHANGED
@@ -29,7 +29,7 @@ module DBF
29
29
  ":string, :limit => #{length}"
30
30
  end
31
31
 
32
- "\"#{underscore(name)}\", #{data_type}\n"
32
+ "\"#{name.underscore}\", #{data_type}\n"
33
33
  end
34
34
 
35
35
  private
data/lib/dbf/globals.rb CHANGED
@@ -25,12 +25,12 @@ module DBF
25
25
  class DBFError < StandardError; end
26
26
 
27
27
  module Helpers
28
- def underscore(camel_cased_word)
29
- camel_cased_word.to_s.gsub(/::/, '/').
30
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
31
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
32
- tr("-", "_").
33
- downcase
34
- end
28
+ # def underscore(camel_cased_word)
29
+ # camel_cased_word.to_s.gsub(/::/, '/').
30
+ # gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
31
+ # gsub(/([a-z\d])([A-Z])/,'\1_\2').
32
+ # tr("-", "_").
33
+ # downcase
34
+ # end
35
35
  end
36
36
  end
data/lib/dbf/record.rb CHANGED
@@ -18,45 +18,40 @@ module DBF
18
18
 
19
19
  def define_accessors
20
20
  @table.columns.each do |column|
21
- underscored_column_name = underscore(column.name)
21
+ underscored_column_name = column.name.underscore
22
22
  unless respond_to?(underscored_column_name)
23
23
  self.class.send :define_method, underscored_column_name do
24
- @attributes[column.name]
24
+ @attributes[column.name.underscore]
25
25
  end
26
26
  end
27
27
  end
28
28
  end
29
29
 
30
30
  def initialize_values(columns)
31
- @attributes = {}
32
- columns.each do |column|
33
- @attributes[column.name] = case column.type
34
- when 'N' # number
35
- column.decimal.zero? ? unpack_string(column).to_i : unpack_string(column).to_f
36
- when 'D' # date
37
- raw = unpack_string(column).strip
38
- unless raw.empty?
39
- parts = raw.match(DATE_REGEXP).captures.map {|n| n.to_i}
40
- begin
41
- Time.gm(*parts)
42
- rescue
43
- Date.new(*parts)
44
- end
45
- end
46
- when 'M' # memo
47
- starting_block = unpack_string(column).to_i
48
- read_memo(starting_block)
49
- when 'L' # logical
50
- unpack_string(column) =~ /^(y|t)$/i ? true : false
51
- when 'I' # integer
52
- unpack_integer(column)
53
- when 'T' # datetime
54
- unpack_datetime(column)
55
- else
56
- unpack_string(column).strip
57
- end
58
- @attributes[underscore(column.name)] = @attributes[column.name]
59
- @attributes
31
+ @attributes = columns.inject({}) do |hash, column|
32
+ hash[column.name.underscore] = typecast_column(column)
33
+ hash
34
+ end
35
+ end
36
+
37
+ def typecast_column(column)
38
+ case column.type
39
+ when 'N' # number
40
+ column.decimal.zero? ? unpack_string(column).to_i : unpack_string(column).to_f
41
+ when 'D' # date
42
+ raw = unpack_string(column).strip
43
+ raw.to_time unless raw.blank?
44
+ when 'M' # memo
45
+ starting_block = unpack_string(column).to_i
46
+ read_memo(starting_block)
47
+ when 'L' # logical
48
+ unpack_string(column) =~ /^(y|t)$/i ? true : false
49
+ when 'I' # integer
50
+ unpack_integer(column)
51
+ when 'T' # datetime
52
+ unpack_datetime(column)
53
+ else
54
+ unpack_string(column).strip
60
55
  end
61
56
  end
62
57
 
data/lib/dbf/table.rb CHANGED
@@ -236,7 +236,7 @@ module DBF
236
236
  end
237
237
 
238
238
  def all_values_match?(record, options)
239
- options.map {|key, value| record.attributes[key.to_s] == value}.all?
239
+ options.map {|key, value| record.attributes[key.to_s.underscore] == value}.all?
240
240
  end
241
241
  end
242
242
 
@@ -22,26 +22,26 @@ describe DBF::Record do
22
22
  table = DBF::Table.new "#{DB_PATH}/dbase_83.dbf"
23
23
  table.column("ID").type.should == "N"
24
24
  table.column("ID").decimal.should == 0
25
- table.records.all? {|record| record.attributes['ID'].should be_kind_of(Integer)}
25
+ table.records.all? {|record| record.attributes['id'].should be_kind_of(Integer)}
26
26
  end
27
27
 
28
28
  it "should typecast number columns with decimals > 0 to Float" do
29
29
  table = DBF::Table.new "#{DB_PATH}/dbase_83.dbf"
30
30
  table.column("ID").type.should == "N"
31
31
  table.column("COST").decimal.should == 2
32
- table.records.all? {|record| record.attributes['COST'].should be_kind_of(Float)}
32
+ table.records.all? {|record| record.attributes['cost'].should be_kind_of(Float)}
33
33
  end
34
34
 
35
35
  it "should typecast memo columns to String" do
36
36
  table = DBF::Table.new "#{DB_PATH}/dbase_83.dbf"
37
37
  table.column("DESC").type.should == "M"
38
- table.records.all? {|record| record.attributes['DESC'].should be_kind_of(String)}
38
+ table.records.all? {|record| record.attributes['desc'].should be_kind_of(String)}
39
39
  end
40
40
 
41
41
  it "should typecast logical columns to True or False" do
42
42
  table = DBF::Table.new "#{DB_PATH}/dbase_30.dbf"
43
43
  table.column("WEBINCLUDE").type.should == "L"
44
- table.records.all? {|record| record.attributes["WEBINCLUDE"].should satisfy {|v| v == true || v == false}}
44
+ table.records.all? {|record| record.attributes["webinclude"].should satisfy {|v| v == true || v == false}}
45
45
  end
46
46
 
47
47
  it "should typecast datetime columns to DateTime" do
@@ -94,7 +94,31 @@ describe DBF::Record do
94
94
 
95
95
  record.send(:read_memo, 5).should be_nil
96
96
  end
97
-
97
+ end
98
+
99
+ describe "#typecase_column" do
100
+ before do
101
+ @table = mock_table
102
+ @column = mock('column')
103
+ @column.stub!(:name).and_return('created')
104
+ @column.stub!(:length).and_return(8)
105
+ @column.stub!(:type).and_return('D')
106
+ @table.stub!(:columns).and_return([@column])
107
+ @record = DBF::Record.new(@table)
108
+ end
109
+
110
+ describe 'when column is type D' do
111
+ it 'should return Time' do
112
+ @record.stub!(:unpack_string).and_return('20080606')
113
+ @record.send(:typecast_column, @column).should == Time.gm(2008, 6, 6)
114
+ end
115
+
116
+ it 'should return Date if Time is out of range' do
117
+ @record.stub!(:unpack_string).and_return('19440606')
118
+ @record.send(:typecast_column, @column).should == Date.new(1944, 6, 6)
119
+ end
120
+ end
121
+
98
122
  end
99
123
 
100
124
  end
@@ -56,7 +56,7 @@ describe DBF::Table do
56
56
  end
57
57
 
58
58
  it "should return matching records when used with options" do
59
- @table.find(:all, "WEIGHT" => 0.0).should == @table.select {|r| r.attributes["WEIGHT"] == 0.0}
59
+ @table.find(:all, "WEIGHT" => 0.0).should == @table.select {|r| r.attributes["weight"] == 0.0}
60
60
  end
61
61
 
62
62
  it "with multiple options should search for all search terms as if using AND" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbf
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.6
4
+ version: 1.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Keith Morrison
@@ -9,9 +9,19 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-08-08 00:00:00 -07:00
12
+ date: 2008-09-02 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: activesupport
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.1.0
24
+ version:
15
25
  - !ruby/object:Gem::Dependency
16
26
  name: hoe
17
27
  type: :development
@@ -22,7 +32,7 @@ dependencies:
22
32
  - !ruby/object:Gem::Version
23
33
  version: 1.7.0
24
34
  version:
25
- description: "DBF is a small fast library for reading dBase, xBase, Clipper and FoxPro database files. It is written completely in Ruby and has no external dependencies. Copyright (c) 2006-2007 Keith Morrison <keithm@infused.org, www.infused.org> * Official project page: http://rubyforge.org/projects/dbf * API Documentation: http://dbf.rubyforge.org/docs * To report bugs: http://www.rubyforge.org/tracker/?group_id=2009 * Questions: Email keithm@infused.org and put DBF somewhere in the subject line"
35
+ description: "DBF is a small fast library for reading dBase, xBase, Clipper and FoxPro database files. It is written completely in Ruby and has no external dependencies. Copyright (c) 2006-2008 Keith Morrison <keithm@infused.org, www.infused.org> * Official project page: http://rubyforge.org/projects/dbf * API Documentation: http://dbf.rubyforge.org/docs * To report bugs: http://www.rubyforge.org/tracker/?group_id=2009 * Questions: Email keithm@infused.org and put DBF somewhere in the subject line"
26
36
  email: keithm@infused.org
27
37
  executables:
28
38
  - dbf