mongomatic 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +4 -4
- data/lib/mongomatic/base.rb +1 -1
- data/lib/mongomatic/errors.rb +1 -1
- data/lib/mongomatic/modifiers.rb +1 -1
- data/test/test_mongomatic.rb +43 -14
- metadata +12 -12
data/README.rdoc
CHANGED
@@ -37,7 +37,7 @@ Mongomatic allows you to map your Ruby objects to Mongo documents. It is designe
|
|
37
37
|
u.valid?
|
38
38
|
=> true
|
39
39
|
u.insert
|
40
|
-
=> BSON::
|
40
|
+
=> BSON::ObjectId('4c32834f0218236321000001')
|
41
41
|
|
42
42
|
User.empty?
|
43
43
|
=> false
|
@@ -48,14 +48,14 @@ Mongomatic allows you to map your Ruby objects to Mongo documents. It is designe
|
|
48
48
|
=> 137
|
49
49
|
|
50
50
|
found = User.find_one({"name" => "Ben Myles"})
|
51
|
-
=> #<User:0x00000101939a48 @doc={"_id"=>BSON::
|
52
|
-
User.find_one(BSON::
|
51
|
+
=> #<User:0x00000101939a48 @doc={"_id"=>BSON::ObjectId('4c32834f0218236321000001'), "name"=>"Ben Myles", "email"=>"me@somewhere.com"}, @removed=false, @is_new=false, @errors=[]>
|
52
|
+
User.find_one(BSON::ObjectId('4c32834f0218236321000001')) == found
|
53
53
|
=> true
|
54
54
|
|
55
55
|
cursor = User.find({"name" => "Ben Myles"})
|
56
56
|
=> #<Mongomatic::Cursor:0x0000010195b4e0 @obj_class=User, @mongo_cursor=<Mongo::Cursor:0x80cadac0 namespace='mongomatic_test.User' @selector={"name"=>"Ben Myles"}>>
|
57
57
|
found = cursor.next
|
58
|
-
=> #<User:0x00000101939a48 @doc={"_id"=>BSON::
|
58
|
+
=> #<User:0x00000101939a48 @doc={"_id"=>BSON::ObjectId('4c32834f0218236321000001'), "name"=>"Ben Myles", "email"=>"me@somewhere.com"}, @removed=false, @is_new=false, @errors=[]>
|
59
59
|
found.remove
|
60
60
|
=> 67
|
61
61
|
User.count
|
data/lib/mongomatic/base.rb
CHANGED
@@ -142,7 +142,7 @@ module Mongomatic
|
|
142
142
|
end
|
143
143
|
|
144
144
|
# Insert the document into the database. Will return false if the document has
|
145
|
-
# already been inserted or is invalid. Returns the generated BSON::
|
145
|
+
# already been inserted or is invalid. Returns the generated BSON::ObjectId
|
146
146
|
# for the new document. Will silently fail if MongoDB is unable to insert the
|
147
147
|
# document, use insert! if you want an error raised instead. Note that this will
|
148
148
|
# require an additional call to the db.
|
data/lib/mongomatic/errors.rb
CHANGED
data/lib/mongomatic/modifiers.rb
CHANGED
@@ -206,7 +206,7 @@ module Mongomatic
|
|
206
206
|
# Adds value to the array only if its not in the array already.<br/>
|
207
207
|
# Or to add many values:<br/>
|
208
208
|
# { $addToSet : { a : { $each : [ 3 , 5 , 6 ] } } }
|
209
|
-
# user.add_to_set("friend_ids", BSON::
|
209
|
+
# user.add_to_set("friend_ids", BSON::ObjectId('...'))
|
210
210
|
def add_to_set(field, val, safe=false)
|
211
211
|
mongo_field = field.to_s
|
212
212
|
field, hash = hash_for_field(mongo_field)
|
data/test/test_mongomatic.rb
CHANGED
@@ -9,7 +9,7 @@ class TestMongomatic < Test::Unit::TestCase
|
|
9
9
|
assert_equal p1, Person.find_one(:name => "Jordan")
|
10
10
|
end
|
11
11
|
|
12
|
-
should "find one with an instance of BSON::
|
12
|
+
should "find one with an instance of BSON::ObjectId" do
|
13
13
|
Person.collection.drop
|
14
14
|
p1 = Person.new(:name => "Jordan")
|
15
15
|
p1.insert
|
@@ -33,15 +33,15 @@ class TestMongomatic < Test::Unit::TestCase
|
|
33
33
|
assert Person.find({"name" => "Jordan"}).empty?
|
34
34
|
end
|
35
35
|
|
36
|
-
should "find one with
|
36
|
+
should "find one with ObjectId or hash only" do
|
37
37
|
Person.collection.drop
|
38
38
|
Person.create_indexes
|
39
39
|
|
40
40
|
p = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
41
|
-
assert p.insert!.is_a?(BSON::
|
41
|
+
assert p.insert!.is_a?(BSON::ObjectId)
|
42
42
|
assert_equal 1, Person.count
|
43
43
|
|
44
|
-
found = Person.find({"_id" => BSON::
|
44
|
+
found = Person.find({"_id" => BSON::ObjectId(p["_id"].to_s)}).next
|
45
45
|
assert_equal found, p
|
46
46
|
|
47
47
|
assert_raise(TypeError) { Person.find_one(p["_id"].to_s) }
|
@@ -49,7 +49,7 @@ class TestMongomatic < Test::Unit::TestCase
|
|
49
49
|
found = Person.find_one({"_id" => p["_id"].to_s})
|
50
50
|
assert_equal found, nil
|
51
51
|
|
52
|
-
found = Person.find_one({"_id" => BSON::
|
52
|
+
found = Person.find_one({"_id" => BSON::ObjectId(p["_id"].to_s)})
|
53
53
|
assert_equal found, p
|
54
54
|
end
|
55
55
|
|
@@ -64,8 +64,8 @@ class TestMongomatic < Test::Unit::TestCase
|
|
64
64
|
Person.collection.drop
|
65
65
|
p1 = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
66
66
|
p2 = Person.new(:name => "Ben2", :birth_year => 1986, :created_at => Time.now.utc, :admin => true)
|
67
|
-
assert p1.insert.is_a?(BSON::
|
68
|
-
assert p2.insert.is_a?(BSON::
|
67
|
+
assert p1.insert.is_a?(BSON::ObjectId)
|
68
|
+
assert p2.insert.is_a?(BSON::ObjectId)
|
69
69
|
assert_equal 2, Person.collection.count
|
70
70
|
assert_equal 2, Person.find.inject(0) { |sum, p| assert p.is_a?(Person); sum += 1 }
|
71
71
|
assert_equal p2, Person.find.max { |p1,p2| p1["birth_year"] <=> p2["birth_year"] }
|
@@ -86,7 +86,7 @@ class TestMongomatic < Test::Unit::TestCase
|
|
86
86
|
|
87
87
|
assert !p.update
|
88
88
|
|
89
|
-
assert p.insert.is_a?(BSON::
|
89
|
+
assert p.insert.is_a?(BSON::ObjectId)
|
90
90
|
|
91
91
|
assert_equal 1, Person.collection.count
|
92
92
|
|
@@ -113,10 +113,10 @@ class TestMongomatic < Test::Unit::TestCase
|
|
113
113
|
should "be able to limit and sort" do
|
114
114
|
Person.collection.drop
|
115
115
|
p = Person.new(:name => "Ben", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
116
|
-
assert p.insert.is_a?(BSON::
|
116
|
+
assert p.insert.is_a?(BSON::ObjectId)
|
117
117
|
assert_equal 1, Person.collection.count
|
118
118
|
p2 = Person.new(:name => "Ben2", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
119
|
-
assert p2.insert.is_a?(BSON::
|
119
|
+
assert p2.insert.is_a?(BSON::ObjectId)
|
120
120
|
assert_equal 2, Person.collection.count
|
121
121
|
|
122
122
|
cursor = Person.find({"_id" => p["_id"]})
|
@@ -146,7 +146,7 @@ class TestMongomatic < Test::Unit::TestCase
|
|
146
146
|
Person.collection.drop
|
147
147
|
1000.upto(2000) do |i|
|
148
148
|
p = Person.new(:name => "Ben#{i}", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
149
|
-
assert p.insert.is_a?(BSON::
|
149
|
+
assert p.insert.is_a?(BSON::ObjectId)
|
150
150
|
end
|
151
151
|
i = 1000
|
152
152
|
Person.find().sort(["name", :asc]).each { |p| assert_equal "Ben#{i}", p["name"]; i += 1 }
|
@@ -161,7 +161,7 @@ class TestMongomatic < Test::Unit::TestCase
|
|
161
161
|
should "be able to merge hashes" do
|
162
162
|
Person.collection.drop
|
163
163
|
p = Person.new(:name => "Ben", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
164
|
-
assert p.insert.is_a?(BSON::
|
164
|
+
assert p.insert.is_a?(BSON::ObjectId)
|
165
165
|
assert_equal 1, Person.collection.count
|
166
166
|
p.merge(:birth_year => 1986)
|
167
167
|
p.update
|
@@ -176,7 +176,7 @@ class TestMongomatic < Test::Unit::TestCase
|
|
176
176
|
assert p.valid?
|
177
177
|
assert_equal [:before_validate, :after_validate], p.callback_tests
|
178
178
|
p.callback_tests = []
|
179
|
-
assert p.insert.is_a?(BSON::
|
179
|
+
assert p.insert.is_a?(BSON::ObjectId)
|
180
180
|
assert_equal [:before_validate, :after_validate, :before_insert, :before_insert_or_update, :after_insert, :after_insert_or_update], p.callback_tests
|
181
181
|
p.callback_tests = []
|
182
182
|
p.update
|
@@ -194,7 +194,7 @@ class TestMongomatic < Test::Unit::TestCase
|
|
194
194
|
Person.create_indexes
|
195
195
|
|
196
196
|
p = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
197
|
-
assert p.insert!.is_a?(BSON::
|
197
|
+
assert p.insert!.is_a?(BSON::ObjectId)
|
198
198
|
assert_equal 1, Person.count
|
199
199
|
|
200
200
|
p = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
|
@@ -519,5 +519,34 @@ class TestMongomatic < Test::Unit::TestCase
|
|
519
519
|
p.valid?
|
520
520
|
assert_nil p.errors.on(:name)
|
521
521
|
end
|
522
|
+
|
523
|
+
should "be able to use errors.on case insensitive" do
|
524
|
+
p = Person.new
|
525
|
+
class << p
|
526
|
+
def validate
|
527
|
+
expectations do
|
528
|
+
be_expected self['name'], ['Name', 'cannot be empty']
|
529
|
+
be_expected self['age'], 'Age cannot be empty'
|
530
|
+
end
|
531
|
+
end
|
532
|
+
end
|
533
|
+
|
534
|
+
p.valid?
|
535
|
+
assert_equal 'Name cannot be empty', p.errors.on('name')
|
536
|
+
assert_equal 'Age cannot be empty', p.errors.on(:age)
|
537
|
+
end
|
522
538
|
|
539
|
+
should "be able to use errors.on with multi word fields" do
|
540
|
+
p = Person.new
|
541
|
+
class << p
|
542
|
+
def validate
|
543
|
+
expectations do
|
544
|
+
be_expected self['hair_color'], 'Hair color must exist'
|
545
|
+
end
|
546
|
+
end
|
547
|
+
end
|
548
|
+
|
549
|
+
p.valid?
|
550
|
+
assert_equal 'Hair color must exist', p.errors.on(:hair_color)
|
551
|
+
end
|
523
552
|
end
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 5
|
8
|
-
-
|
9
|
-
version: 0.5.
|
8
|
+
- 1
|
9
|
+
version: 0.5.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Ben Myles
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-
|
17
|
+
date: 2010-09-02 00:00:00 -07:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -38,13 +38,13 @@ dependencies:
|
|
38
38
|
requirement: &id002 !ruby/object:Gem::Requirement
|
39
39
|
none: false
|
40
40
|
requirements:
|
41
|
-
- - "
|
41
|
+
- - "="
|
42
42
|
- !ruby/object:Gem::Version
|
43
43
|
segments:
|
44
44
|
- 1
|
45
45
|
- 0
|
46
|
-
-
|
47
|
-
version: 1.0.
|
46
|
+
- 7
|
47
|
+
version: 1.0.7
|
48
48
|
type: :runtime
|
49
49
|
version_requirements: *id002
|
50
50
|
- !ruby/object:Gem::Dependency
|
@@ -53,13 +53,13 @@ dependencies:
|
|
53
53
|
requirement: &id003 !ruby/object:Gem::Requirement
|
54
54
|
none: false
|
55
55
|
requirements:
|
56
|
-
- - "
|
56
|
+
- - "="
|
57
57
|
- !ruby/object:Gem::Version
|
58
58
|
segments:
|
59
59
|
- 1
|
60
60
|
- 0
|
61
|
-
-
|
62
|
-
version: 1.0.
|
61
|
+
- 7
|
62
|
+
version: 1.0.7
|
63
63
|
type: :runtime
|
64
64
|
version_requirements: *id003
|
65
65
|
- !ruby/object:Gem::Dependency
|
@@ -68,13 +68,13 @@ dependencies:
|
|
68
68
|
requirement: &id004 !ruby/object:Gem::Requirement
|
69
69
|
none: false
|
70
70
|
requirements:
|
71
|
-
- - "
|
71
|
+
- - "="
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
segments:
|
74
74
|
- 1
|
75
75
|
- 0
|
76
|
-
-
|
77
|
-
version: 1.0.
|
76
|
+
- 8
|
77
|
+
version: 1.0.8
|
78
78
|
type: :runtime
|
79
79
|
version_requirements: *id004
|
80
80
|
- !ruby/object:Gem::Dependency
|