activemdb 0.2.1 → 0.2.2

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.
@@ -1,3 +1,9 @@
1
+ == 0.2.2 / 2007-04-18
2
+
3
+ Reworked conditions_hash call to handle either field or method names.
4
+ Methodized the attributes hash for Base.
5
+ Added much documentation
6
+
1
7
  == 0.2.1 / 2007-04-10
2
8
 
3
9
  Added typecasting so that the strings returned from mdb-sql become happy appropriate Ruby things.
@@ -1,6 +1,6 @@
1
1
  $:.unshift(File.dirname(__FILE__))
2
2
  module ActiveMDB
3
- VERSION = '0.2.1'
3
+ VERSION = '0.2.2'
4
4
  end
5
5
 
6
6
 
@@ -128,34 +128,39 @@ module ActiveMDB
128
128
  # e.g. search_with_hash(:first_name => 'Matthew', :last_name => 'King')
129
129
  def conditions_from_hash(hash)
130
130
  MDBTools.compile_conditions(hash) do |column_name, value|
131
- column = column_for(column_name)
131
+ column = column_for_method(column_name) || column_for_field(column_name)
132
+ raise ArgumentError, "No column corresponding to #{column_name}" unless column
132
133
  case column.klass.to_s
133
134
  when 'Fixnum', 'Float'
134
- "#{column_name} = #{value}"
135
+ "#{column.name} = #{value}"
135
136
  when 'String'
136
- "#{column_name} LIKE '%#{value}%'"
137
+ "#{column.name} LIKE '%#{value}%'"
137
138
  when 'Object'
138
139
  value = value ? 1 : 0
139
- "#{column_name} IS #{value}"
140
+ "#{column.name} IS #{value}"
140
141
  end
141
142
  end
142
143
  end
143
144
 
144
145
  # given the name of a column, return the Column object
145
- def column_for(column_name)
146
+ def column_for_field(column_name)
146
147
  columns.detect {|c| c.name == column_name.to_s}
147
148
  end
148
149
 
150
+ def column_for_method(method_name)
151
+ columns.detect {|c| c.method_name == method_name.to_s}
152
+ end
153
+
149
154
  private
150
155
 
151
156
  def instantiate(record)
152
157
  new_hash = {}
153
158
  record.each do |name,value|
154
- begin
155
- new_hash[name] = column_for(name).type_cast(value)
156
- rescue
157
- raise "No column for #{name}"
158
- end
159
+ # begin
160
+ new_hash[MDBTools.methodize(name)] = column_for_field(name).type_cast(value)
161
+ # rescue
162
+ # raise "No column for #{name}"
163
+ # end
159
164
  end
160
165
  self.new new_hash
161
166
  end
@@ -166,7 +171,7 @@ module ActiveMDB
166
171
  private
167
172
 
168
173
  def method_missing(method_id, *args, &block)
169
- method_name = method_id.to_s
174
+ method_name = MDBTools.methodize(method_id.to_s)
170
175
  if @attributes.include?(method_name)
171
176
  value = @attributes[method_name]
172
177
  else
@@ -46,6 +46,10 @@ class Column
46
46
  end
47
47
  end
48
48
 
49
+ def self.string_to_time(string)
50
+ string
51
+ end
52
+
49
53
  # provided any argument, returns the mdb-tools version
50
54
  # of truth (which is to say, 1 or 0)
51
55
  def self.value_to_boolean(value)
@@ -18,6 +18,13 @@ class BaseTest < Test::Unit::TestCase
18
18
  set_primary_key 'Room'
19
19
  end
20
20
 
21
+ class Person < ActiveMDB::Base
22
+ set_mdb_file RETREAT_DB
23
+ set_table_name 'tblIndividData'
24
+ set_primary_key 'IndKey'
25
+ end
26
+
27
+
21
28
  def test_setting_mdb_file
22
29
  assert_equal TEST_DB, Employee.mdb_file
23
30
  end
@@ -60,10 +67,12 @@ class BaseTest < Test::Unit::TestCase
60
67
  def test_instantiate
61
68
  hash = {"Department"=>"Engineering", "Gender"=>"M", "Room"=>"6072", "Title"=>"Programmer", "Emp_Id"=>"1045", "First_Name"=>"Robert", "Last_Name"=>"Weinfeld"}
62
69
  record = Employee.send(:instantiate, hash)
63
- assert_equal hash, record.instance_variable_get('@attributes')
70
+ methodized_hash = {}
71
+ hash.each {|k,v| methodized_hash[MDBTools.methodize(k)] = v}
72
+ assert_equal methodized_hash, record.instance_variable_get('@attributes')
64
73
  hash = {"Area"=>"135.38","Entity_Handle"=>"9D7A","Room"=>"6004","Room_Type"=>"STOR-GEN"}
65
74
  record = Room.send(:instantiate, hash)
66
- assert_kind_of Float, record.instance_variable_get('@attributes')['Area']
75
+ assert_kind_of Float, record.instance_variable_get('@attributes')['area']
67
76
  end
68
77
 
69
78
  def test_find_from_hash
@@ -97,10 +106,15 @@ class BaseTest < Test::Unit::TestCase
97
106
  assert_equal 2, Employee.find_all(:First_Name => 'G').size
98
107
  end
99
108
 
100
- def test_column_for
101
- assert_kind_of Column, Employee.column_for(:Room)
102
- assert_nil Employee.column_for('foo')
109
+ def test_column_for_field
110
+ assert_kind_of Column, Employee.column_for_field(:Room)
111
+ assert_nil Employee.column_for_field('foo')
103
112
  end
104
113
 
114
+ def test_column_for_method
115
+ assert_kind_of Column, Employee.column_for_method(:room)
116
+ end
117
+
118
+
105
119
 
106
120
  end
@@ -16,4 +16,11 @@ class ColumnTest < Test::Unit::TestCase
16
16
 
17
17
 
18
18
 
19
+ # def test_string_to_time
20
+ # time = '05/18/76 00:00:00'
21
+ # t2 = "02/17/47 00:00:00"
22
+ # assert_equal 1976, Column.string_to_time(time).year
23
+ # assert_equal 5, Column.string_to_time(time).month
24
+ # end
25
+
19
26
  end
@@ -1,5 +1,6 @@
1
1
  require 'test/unit'
2
2
  TEST_DB = File.join(File.dirname(__FILE__), '..', 'db', 'sample.mdb')
3
+ RETREAT_DB = File.join(File.dirname(__FILE__), '..', 'db', 'retreat.mdb')
3
4
  NOT_A_DB = File.join(File.dirname(__FILE__), '..', 'db', 'not_an_mdb.txt')
4
5
 
5
6
  class Test::Unit::TestCase
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.9.2
3
3
  specification_version: 1
4
4
  name: activemdb
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.1
7
- date: 2007-04-10 00:00:00 -05:00
6
+ version: 0.2.2
7
+ date: 2007-04-18 00:00:00 -05:00
8
8
  summary: ActiveRecordy wrapper around MDB Tools, allowing POSIX platforms to read MS Access (.mdb) files
9
9
  require_paths:
10
10
  - lib