mongomatic 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Mongomatic allows you to map your Ruby objects to Mongo documents. It is designed to be fast and simple.
4
4
 
5
- == Usage
5
+ == Basic Usage
6
6
 
7
7
  require 'mongomatic'
8
8
 
@@ -10,7 +10,7 @@ Mongomatic allows you to map your Ruby objects to Mongo documents. It is designe
10
10
  validates_presence_of :name, :email
11
11
  end
12
12
 
13
- # set the db settings for all models:
13
+ # set the db for all models:
14
14
  Mongomatic.db = Mongo::Connection.new.db("mongomatic_test")
15
15
  # or you can set it for a specific model:
16
16
  User.db = Mongo::Connection.new.db("mongomatic_test_user")
@@ -46,7 +46,30 @@ Mongomatic allows you to map your Ruby objects to Mongo documents. It is designe
46
46
  cursor.count
47
47
  => 0
48
48
  cursor.next
49
- => nil
49
+ => nil
50
+
51
+ found = User.find_one({"name" => "Ben Myles"})
52
+ => #<User:0x00000100ccd408 @doc={"_id"=>BSON::ObjectID('4c32c3720218236526000001'), "name"=>"Ben Myles", "email"=>"me@somewhere.com"}, @removed=false>
53
+ found = User.find_one(BSON::ObjectID('4c32c3720218236526000001'))
54
+ => #<User:0x00000100ccd408 @doc={"_id"=>BSON::ObjectID('4c32c3720218236526000001'), "name"=>"Ben Myles", "email"=>"me@somewhere.com"}, @removed=false>
55
+
56
+ == Indexes
57
+
58
+ Mongomatic doesn't do anything special to support indexes, but here's the suggested convention:
59
+
60
+ class Person < Mongomatic::Base
61
+ validates_presence_of :name, :email
62
+
63
+ class << self
64
+ def create_indexes
65
+ collection.create_index("email", :unique => true)
66
+ end
67
+ end
68
+ end
69
+
70
+ You can run Person.create_indexes whenever you add new indexes, it won't throw an error if they already exist.
71
+
72
+ If you have defined a unique index and want Mongomatic to raise an exception on a duplicate insert you need to use insert_safe or update_safe. The error thrown will be Mongo::OperationFailure. See the test suite for examples.
50
73
 
51
74
  == Note on Patches/Pull Requests
52
75
 
@@ -26,8 +26,7 @@ module Mongomatic
26
26
  Mongomatic::Cursor.new(self, collection.find(query, opts))
27
27
  end
28
28
 
29
- def find_one(query = nil, opts = {})
30
- query = BSON::ObjectID(query) if query.is_a? String
29
+ def find_one(query={}, opts={})
31
30
  return nil unless doc = self.collection.find_one(query, opts)
32
31
  self.new(doc)
33
32
  end
@@ -98,6 +97,11 @@ module Mongomatic
98
97
  ret
99
98
  end
100
99
 
100
+ def insert_safe(opts={})
101
+ opts.merge!(:safe => true)
102
+ insert(opts)
103
+ end
104
+
101
105
  def update(opts={},update_doc=@doc)
102
106
  return false if new? || removed? || !valid?
103
107
  self.send(:before_update) if self.respond_to?(:before_update)
@@ -108,6 +112,11 @@ module Mongomatic
108
112
  ret
109
113
  end
110
114
 
115
+ def update_safe(opts={},update_doc=@doc)
116
+ opts.merge!(:safe => true)
117
+ update(opts,update_doc)
118
+ end
119
+
111
120
  def remove(opts={})
112
121
  return false if new?
113
122
  self.send(:before_remove) if self.respond_to?(:before_remove)
@@ -118,6 +127,11 @@ module Mongomatic
118
127
  ret
119
128
  end
120
129
 
130
+ def remove_safe(opts={})
131
+ opts.merge!(:safe => true)
132
+ remove(opts)
133
+ end
134
+
121
135
  def to_hash
122
136
  @doc || {}
123
137
  end
data/test/helper.rb CHANGED
@@ -13,6 +13,10 @@ class Person < Mongomatic::Base
13
13
  validates_presence_of :name
14
14
  attr_accessor :callback_tests
15
15
 
16
+ def self.create_indexes
17
+ collection.create_index("name", :unique => true)
18
+ end
19
+
16
20
  def before_validate
17
21
  self.callback_tests ||= []
18
22
  self.callback_tests << :before_validate
@@ -2,35 +2,50 @@ require 'helper'
2
2
 
3
3
  class TestMongomatic < Test::Unit::TestCase
4
4
  should "find one with a query" do
5
- Person.collection.remove
5
+ Person.collection.drop
6
6
  p1 = Person.new(:name => "Jordan")
7
7
  p1.insert
8
8
 
9
9
  assert_equal p1, Person.find_one(:name => "Jordan")
10
10
  end
11
+
11
12
  should "find one with an instance of BSON::ObjectID" do
12
- Person.collection.remove
13
+ Person.collection.drop
13
14
  p1 = Person.new(:name => "Jordan")
14
15
  p1.insert
15
16
 
16
17
  assert_equal p1, Person.find_one(p1['_id'])
17
18
  end
18
- should "find one with a string" do
19
- Person.collection.remove
20
- p1 = Person.new(:name => "Jordan")
21
- p1.insert
19
+
20
+ should "find one with ObjectID or hash only" do
21
+ Person.collection.drop
22
+ Person.create_indexes
23
+
24
+ p = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
25
+ assert p.insert_safe.is_a?(BSON::ObjectID)
26
+ assert_equal 1, Person.count
27
+
28
+ found = Person.find({"_id" => BSON::ObjectID(p["_id"].to_s)}).next
29
+ assert_equal found, p
30
+
31
+ assert_raise(TypeError) { Person.find_one(p["_id"].to_s) }
32
+
33
+ found = Person.find_one({"_id" => p["_id"].to_s})
34
+ assert_equal found, nil
22
35
 
23
- assert_equal p1, Person.find_one(p1['_id'].to_s)
36
+ found = Person.find_one({"_id" => BSON::ObjectID(p["_id"].to_s)})
37
+ assert_equal found, p
24
38
  end
39
+
25
40
  should "return an instance of class when finding one" do
26
- Person.collection.remove
41
+ Person.collection.drop
27
42
  p1 = Person.new(:name => "Jordan")
28
43
  p1.insert
29
44
 
30
45
  assert_equal Person, Person.find_one(:name => "Jordan").class
31
46
  end
32
47
  should "work with enumerable methods" do
33
- Person.collection.remove
48
+ Person.collection.drop
34
49
  p1 = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
35
50
  p2 = Person.new(:name => "Ben2", :birth_year => 1986, :created_at => Time.now.utc, :admin => true)
36
51
  assert p1.insert.is_a?(BSON::ObjectID)
@@ -41,7 +56,7 @@ class TestMongomatic < Test::Unit::TestCase
41
56
  end
42
57
 
43
58
  should "be able to insert, update, remove documents" do
44
- Person.collection.remove
59
+ Person.collection.drop
45
60
 
46
61
  p = Person.new
47
62
 
@@ -80,7 +95,7 @@ class TestMongomatic < Test::Unit::TestCase
80
95
  end
81
96
 
82
97
  should "be able to limit and sort" do
83
- Person.collection.remove
98
+ Person.collection.drop
84
99
  p = Person.new(:name => "Ben", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
85
100
  assert p.insert.is_a?(BSON::ObjectID)
86
101
  assert_equal 1, Person.collection.count
@@ -112,7 +127,7 @@ class TestMongomatic < Test::Unit::TestCase
112
127
  end
113
128
 
114
129
  should "cursor implements enumerable" do
115
- Person.collection.remove
130
+ Person.collection.drop
116
131
  1000.upto(2000) do |i|
117
132
  p = Person.new(:name => "Ben#{i}", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
118
133
  assert p.insert.is_a?(BSON::ObjectID)
@@ -128,7 +143,7 @@ class TestMongomatic < Test::Unit::TestCase
128
143
  end
129
144
 
130
145
  should "be able to merge hashes" do
131
- Person.collection.remove
146
+ Person.collection.drop
132
147
  p = Person.new(:name => "Ben", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
133
148
  assert p.insert.is_a?(BSON::ObjectID)
134
149
  assert_equal 1, Person.collection.count
@@ -154,4 +169,18 @@ class TestMongomatic < Test::Unit::TestCase
154
169
  p.remove
155
170
  assert_equal [:before_remove, :after_remove], p.callback_tests
156
171
  end
172
+
173
+ should "raise an error on unique index dup insert" do
174
+ Person.collection.drop
175
+ Person.create_indexes
176
+
177
+ p = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
178
+ assert p.insert_safe.is_a?(BSON::ObjectID)
179
+ assert_equal 1, Person.count
180
+
181
+ p = Person.new(:name => "Ben1", :birth_year => 1984, :created_at => Time.now.utc, :admin => true)
182
+ assert_raise(Mongo::OperationFailure) { p.insert_safe }
183
+
184
+ assert_equal 1, Person.count
185
+ end
157
186
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 1
9
- version: 0.1.1
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ben Myles
@@ -21,7 +21,6 @@ dependencies:
21
21
  name: shoulda
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
- none: false
25
24
  requirements:
26
25
  - - ">="
27
26
  - !ruby/object:Gem::Version
@@ -36,7 +35,6 @@ dependencies:
36
35
  name: bson
37
36
  prerelease: false
38
37
  requirement: &id002 !ruby/object:Gem::Requirement
39
- none: false
40
38
  requirements:
41
39
  - - ">="
42
40
  - !ruby/object:Gem::Version
@@ -51,7 +49,6 @@ dependencies:
51
49
  name: bson_ext
52
50
  prerelease: false
53
51
  requirement: &id003 !ruby/object:Gem::Requirement
54
- none: false
55
52
  requirements:
56
53
  - - ">="
57
54
  - !ruby/object:Gem::Version
@@ -66,7 +63,6 @@ dependencies:
66
63
  name: mongo
67
64
  prerelease: false
68
65
  requirement: &id004 !ruby/object:Gem::Requirement
69
- none: false
70
66
  requirements:
71
67
  - - ">="
72
68
  - !ruby/object:Gem::Version
@@ -81,7 +77,6 @@ dependencies:
81
77
  name: activesupport
82
78
  prerelease: false
83
79
  requirement: &id005 !ruby/object:Gem::Requirement
84
- none: false
85
80
  requirements:
86
81
  - - ">="
87
82
  - !ruby/object:Gem::Version
@@ -130,8 +125,6 @@ files:
130
125
  - lib/mongomatic/validatable/validations/validation_base.rb
131
126
  - LICENSE
132
127
  - README.rdoc
133
- - test/helper.rb
134
- - test/test_mongomatic.rb
135
128
  has_rdoc: true
136
129
  homepage: http://github.com/benmyles/mongomatic
137
130
  licenses: []
@@ -142,7 +135,6 @@ rdoc_options:
142
135
  require_paths:
143
136
  - lib
144
137
  required_ruby_version: !ruby/object:Gem::Requirement
145
- none: false
146
138
  requirements:
147
139
  - - ">="
148
140
  - !ruby/object:Gem::Version
@@ -150,7 +142,6 @@ required_ruby_version: !ruby/object:Gem::Requirement
150
142
  - 0
151
143
  version: "0"
152
144
  required_rubygems_version: !ruby/object:Gem::Requirement
153
- none: false
154
145
  requirements:
155
146
  - - ">="
156
147
  - !ruby/object:Gem::Version
@@ -160,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
151
  requirements: []
161
152
 
162
153
  rubyforge_project:
163
- rubygems_version: 1.3.7
154
+ rubygems_version: 1.3.6
164
155
  signing_key:
165
156
  specification_version: 3
166
157
  summary: Mongomatic is a simple Ruby object mapper for Mongo