mongoose 0.2.0 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,6 +3,24 @@
3
3
  module Mongoose
4
4
 
5
5
  module Util
6
+ SINGULAR_TO_PLURAL = Hash.new { |h, k| h[k] = k.to_s + 's' }
7
+ PLURAL_TO_SINGULAR = Hash.new do |h, k|
8
+ h[k] = if md = k.to_s.match(/\A(.*)s\z/)
9
+ md[1]
10
+ else
11
+ raise(ArgumentError, "Please use " +
12
+ "plural_form for this special case")
13
+ end
14
+ end
15
+
16
+ SPECIAL_PLURALIZATION_CASES = { "child" => "children", "person" => "people",
17
+ "mouse" => "mice" }
18
+
19
+ SINGULAR_TO_PLURAL.update SPECIAL_PLURALIZATION_CASES
20
+ PLURAL_TO_SINGULAR.update SPECIAL_PLURALIZATION_CASES.invert
21
+
22
+
23
+
6
24
  # converts from things like user_name to things like UserName
7
25
  def self.us_case_to_class_case(name)
8
26
  name.to_s.split(/_/).map do |word|
@@ -12,22 +30,17 @@ end
12
30
 
13
31
  # converts from things like UserName to things like user_name
14
32
  def self.class_case_to_us_case(name)
15
- decapitalized = name.to_s.sub(/^./) { |match| match.downcase }
16
- decapitalized.gsub(/[A-Z]/) { |match| "_" + match.downcase }
33
+ name.split(/(?=[A-Z])/).map { |s| s.downcase }.join('_')
17
34
  end
18
35
 
19
36
  # Adds an s
20
37
  def self.pluralize(name)
21
- name.to_s + 's'
38
+ SINGULAR_TO_PLURAL[name]
22
39
  end
23
40
 
24
41
  # chops an s
25
42
  def self.singularize(name)
26
- if md = name.to_s.match(/^(.+?)s\z/)
27
- md[1]
28
- else
29
- raise RuntimeError, "#{name} is not plural"
30
- end
43
+ PLURAL_TO_SINGULAR[name]
31
44
  end
32
45
 
33
46
  def self.col_name_for_class(name)
@@ -21,6 +21,16 @@ class Flight < Mongoose::Table
21
21
  belongs_to :pilot
22
22
  end
23
23
 
24
+ class Ox < Mongoose::Table
25
+ plural_form 'oxen'
26
+ belongs_to :farmer
27
+ end
28
+
29
+ class Farmer < Mongoose::Table
30
+ has_many :oxen
31
+ end
32
+
33
+
24
34
  class TestRelations < Test::Unit::TestCase
25
35
  def setup
26
36
  @db = Mongoose::Database.new(:path => Dir.tmpdir)
@@ -44,6 +54,14 @@ class TestRelations < Test::Unit::TestCase
44
54
  tbl.add_column(:pilot_id, :integer)
45
55
  end
46
56
 
57
+ @db.create_table(:ox) do |tbl|
58
+ tbl.add_column(:farmer_id, :integer)
59
+ end
60
+
61
+ @db.create_table(:farmer) do |tbl|
62
+ tbl.add_column(:name, :string)
63
+ end
64
+
47
65
  @doolittle = Pilot.create :name => 'Doolittle, James', :years_flying => 15
48
66
  @amelia = Pilot.create :name => 'Earhart, Amelia', :years_flying => 10
49
67
 
@@ -60,12 +78,17 @@ class TestRelations < Test::Unit::TestCase
60
78
  :destination => 'Phillipines'
61
79
  Flight.create :pilot_id => @amelia.id, :origin => 'China',
62
80
  :destination => 'Unknown'
81
+
82
+ @farmer = Farmer.create :name => 'Old McDonald'
83
+ Ox.create :farmer_id => @farmer.id
63
84
  end
64
85
 
65
86
  def teardown
66
87
  @db.drop_table(:pilot)
67
88
  @db.drop_table(:plane)
68
89
  @db.drop_table(:flight)
90
+ @db.drop_table(:farmer)
91
+ @db.drop_table(:ox)
69
92
  @db.close
70
93
  end
71
94
 
@@ -93,10 +116,14 @@ class TestRelations < Test::Unit::TestCase
93
116
 
94
117
  def test_belongs_to_002
95
118
  red_baron = Pilot.create :name => 'Baron, The Red', :years_flying => 50
96
- spitfire = Plane.find(:first) { name == 'Spitfire'}
119
+ spitfire = Plane.find(:first) { |p| p.name == 'Spitfire' }
97
120
  spitfire.pilot = red_baron
98
121
 
99
122
  assert('Baron, The Red', spitfire.pilot.name)
100
123
  assert_equal('Spitfire', red_baron.plane.name)
101
124
  end
125
+
126
+ def test_plural_form
127
+ assert_equal(1, @farmer.oxen.size)
128
+ end
102
129
  end
@@ -35,6 +35,9 @@ class TestTable < Test::Unit::TestCase
35
35
  def teardown
36
36
  @db.drop_table(:plane)
37
37
  @db.close
38
+
39
+ File.delete(File.join(Dir.tmpdir, 'plane.csv')) if File.exists?(File.join(
40
+ Dir.tmpdir, 'plane.csv'))
38
41
  end
39
42
 
40
43
  def test_table_exists
@@ -44,6 +47,14 @@ class TestTable < Test::Unit::TestCase
44
47
  assert_equal(5, Plane.columns.size)
45
48
  end
46
49
 
50
+ def test_add_record_from_hash
51
+ rec = Plane.new(:name => 'Zero', :country => 'Japan', :speed => 377,
52
+ :range => 888)
53
+ rec.save
54
+
55
+ assert_equal(1, Plane.find_all_by_name('Zero').size)
56
+ end
57
+
47
58
  def test_add_record
48
59
  rec = Plane.new
49
60
  rec.name = 'Zero'
@@ -62,6 +73,14 @@ class TestTable < Test::Unit::TestCase
62
73
  assert_equal(888, rec2.range)
63
74
  end
64
75
 
76
+ def test_update_attributes
77
+ rec = Plane.find(2)
78
+ rec.update_attributes(:country => 'England', :speed => 119)
79
+
80
+ assert_equal('England', Plane.find(2).country)
81
+ assert_equal(119, Plane.find(2).speed)
82
+ end
83
+
65
84
  def test_update_record_001
66
85
  rec = Plane.find(2)
67
86
  rec.country = 'England'
@@ -83,7 +102,7 @@ class TestTable < Test::Unit::TestCase
83
102
  end
84
103
 
85
104
  def find_001
86
- rec = Plane.find(:first) { name == 'P-51' }
105
+ rec = Plane.find(:first) { |p| p.name == 'P-51' }
87
106
 
88
107
  assert(rec.is_a?(Plane))
89
108
  assert_equal(rec.name, 'P-51')
@@ -99,7 +118,7 @@ class TestTable < Test::Unit::TestCase
99
118
  end
100
119
 
101
120
  def find_002
102
- recs = Plane.find { name == 'P-51' }
121
+ recs = Plane.find { |p| p.name == 'P-51' }
103
122
 
104
123
  assert_equal(recs.size, 1)
105
124
  assert(recs.first.is_a?(Plane))
@@ -122,30 +141,61 @@ class TestTable < Test::Unit::TestCase
122
141
  end
123
142
 
124
143
  def find_003
125
- recs = Plane.find do
126
- any do
127
- country == 'USA'
128
- country == 'Great Britain'
144
+ recs = Plane.find do |p|
145
+ p.any do
146
+ p.country == 'USA'
147
+ p.country == 'Great Britain'
129
148
  end
130
149
  end
131
150
  assert_equal(recs.size, 3)
132
151
 
133
- recs = Plane.find do
134
- any do
135
- country == 'USA'
136
- country == 'Great Britain'
152
+ recs = Plane.find do |p|
153
+ p.any do
154
+ p.country == 'USA'
155
+ p.country == 'Great Britain'
137
156
  end
138
- speed > 400
157
+ p.speed > 400
139
158
  end
140
159
  assert_equal(recs.size, 2)
141
160
 
142
- recs = Plane.find do
143
- any do
144
- country == 'USA'
145
- country == 'Great Britain'
161
+ recs = Plane.find do |p|
162
+ p.speed > 400
163
+ p.any do
164
+ p.country == 'USA'
165
+ p.country == 'Great Britain'
146
166
  end
147
- speed > 400
148
- range > 1000
167
+ end
168
+ assert_equal(recs.size, 2)
169
+
170
+ recs = Plane.find do |p|
171
+ p.any do
172
+ p.country == 'USA'
173
+ p.country == 'Great Britain'
174
+ end
175
+ p.speed > 400
176
+ p.range > 1000
177
+ end
178
+ assert_equal(recs.size, 1)
179
+ assert_equal(recs.first.name, 'P-51')
180
+
181
+ recs = Plane.find do |p|
182
+ p.speed > 400
183
+ p.range > 1000
184
+ p.any do
185
+ p.country == 'USA'
186
+ p.country == 'Great Britain'
187
+ end
188
+ end
189
+ assert_equal(recs.size, 1)
190
+ assert_equal(recs.first.name, 'P-51')
191
+
192
+ recs = Plane.find do |p|
193
+ p.speed > 400
194
+ p.any do
195
+ p.country == 'USA'
196
+ p.country == 'Great Britain'
197
+ end
198
+ p.range > 1000
149
199
  end
150
200
  assert_equal(recs.size, 1)
151
201
  assert_equal(recs.first.name, 'P-51')
@@ -161,23 +211,23 @@ class TestTable < Test::Unit::TestCase
161
211
  end
162
212
 
163
213
  def find_004
164
- recs = Plane.find { name.one_of('P-51', 'ME-109') }
214
+ recs = Plane.find { |p| p.name.one_of('P-51', 'ME-109') }
165
215
  assert_equal(recs.size, 2)
166
216
 
167
- recs = Plane.find { country.one_of('USA', 'Germany') }
217
+ recs = Plane.find { |p| p.country.one_of('USA', 'Germany') }
168
218
  assert_equal(recs.size, 3)
169
219
 
170
- recs = Plane.find { speed.between(351, 400) }
220
+ recs = Plane.find { |p| p.speed.between(351, 400) }
171
221
  assert_equal(0, recs.size)
172
222
 
173
- recs = Plane.find { speed.between(351, 400, true) }
223
+ recs = Plane.find { |p| p.speed.between(351, 400, true) }
174
224
  assert_equal(1, recs.size)
175
225
  assert_equal('ME-109', recs.first.name)
176
226
 
177
- recs = Plane.find { range.between(450, 501) }
227
+ recs = Plane.find { |p| p.range.between(450, 501) }
178
228
  assert_equal(1, recs.size)
179
229
 
180
- recs = Plane.find { range.between(450, 501, false, true) }
230
+ recs = Plane.find { |p| p.range.between(450, 501, false, true) }
181
231
  assert_equal(2, recs.size)
182
232
  end
183
233
 
@@ -190,12 +240,82 @@ class TestTable < Test::Unit::TestCase
190
240
  find_004
191
241
  end
192
242
 
193
- def test_destroy
194
- rec = Plane.find(:first) { name == 'P-51' }
243
+ def find_005
244
+ assert_equal('P-51', Plane.find(1).name)
245
+
246
+ recs = Plane.find(1,3)
247
+ assert_equal(2, recs.size)
248
+ end
249
+
250
+ def test_find_005
251
+ find_005
252
+
253
+ @db.close
254
+ @db = Mongoose::Database.new(:path => Dir.tmpdir)
255
+
256
+ find_005
257
+ end
258
+
259
+ def test_find_by_001
260
+ assert_equal('Spitfire', Plane.find_by_name('Spitfire').name)
261
+ assert_equal('P-51', Plane.find_by_id(1).name)
262
+ end
263
+
264
+ def test_find_all_by_001
265
+ assert(2, Plane.find_all_by_country('USA').size)
266
+ assert('P-38', Plane.find_all_by_country('USA', :order => :name).first.name)
267
+ end
268
+
269
+ def test_destroy_001
270
+ rec = Plane.find(:first) { |p| p.name == 'P-51' }
195
271
  rec.destroy
196
272
 
197
273
  recs = Plane.find
198
274
 
199
275
  assert_equal(recs.size, 3)
200
276
  end
277
+
278
+ def test_destroy_002
279
+ Plane.destroy(1)
280
+
281
+ assert_nil(Plane.find(1))
282
+ end
283
+
284
+ def test_content_columns
285
+ assert_equal([:name,:country,:speed,:range],
286
+ Plane.content_columns.collect {|c| c.name})
287
+ end
288
+
289
+ def test_exists?
290
+ assert(Plane.exists?(1))
291
+ assert(!Plane.exists?(0))
292
+ end
293
+
294
+ def test_limit_001
295
+ assert_equal(2, Plane.find(:limit => 2).size)
296
+ end
297
+
298
+ def test_order_001
299
+ assert_equal('ME-109', Plane.find(:order => :name).first.name)
300
+ assert_equal('ME-109', Plane.find(:first, :order => :name).name)
301
+ assert_equal('P-38', Plane.find(:order => [:country, :name])[2].name)
302
+ assert_equal('P-38', Plane.find(:first, :offset => 3,
303
+ :order => [:country, :name]).name)
304
+ assert_equal('P-51', Plane.find(:order => [:country, -:name])[2].name)
305
+ assert_equal('P-51', Plane.find(:first, :offset => 3,
306
+ :order => [:country, -:name]).name)
307
+ end
308
+
309
+ def test_offset_001
310
+ assert_equal('Spitfire', Plane.find(:first, :order => :country, :offset => 2
311
+ ).name)
312
+ end
313
+
314
+ def test_export_import
315
+ Plane.export(File.join(Dir.tmpdir, 'plane.csv'))
316
+ assert(File.exists?(File.join(Dir.tmpdir, 'plane.csv')))
317
+
318
+ Plane.import(File.join(Dir.tmpdir, 'plane.csv'))
319
+ assert_equal(4, Plane.find.size)
320
+ end
201
321
  end
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.11
3
3
  specification_version: 1
4
4
  name: mongoose
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.2.0
7
- date: 2006-07-21 00:00:00 -04:00
6
+ version: 0.2.5
7
+ date: 2006-07-25 00:00:00 -04:00
8
8
  summary: Mongoose is a pure_ruby database management system.
9
9
  require_paths:
10
10
  - lib
@@ -36,6 +36,10 @@ files:
36
36
  - lib/mongoose/table.rb
37
37
  - lib/mongoose/util.rb
38
38
  - lib/mongoose/column.rb
39
+ - lib/mongoose/query.rb
40
+ - lib/mongoose/error.rb
41
+ - bin/mongoose_import.rb
42
+ - bin/mongoose_export.rb
39
43
  - README
40
44
  - changes.txt
41
45
  - test/tc_database.rb
@@ -51,8 +55,9 @@ rdoc_options: []
51
55
 
52
56
  extra_rdoc_files: []
53
57
 
54
- executables: []
55
-
58
+ executables:
59
+ - mongoose_import.rb
60
+ - mongoose_export.rb
56
61
  extensions: []
57
62
 
58
63
  requirements: