light_mongo 0.3.0 → 0.4.0

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.
data/README.md CHANGED
@@ -13,6 +13,10 @@ LightMongo is best installed via `gem install light_mongo` and required as norma
13
13
  It is dependent on the gem `mongo`, which is the MongoDB Ruby driver.
14
14
  For performance reasons I would recommend also installing `mongo_ext`, the C extensions for the driver.
15
15
 
16
+ Rails
17
+ -----
18
+ Please see [LightMongo-Rails][lm_rails] for an ActionPack-compatible bridge.
19
+
16
20
  The problem
17
21
  -----------
18
22
  Developers occasionally encounter a domain which defies simple modelling in an ActiveRecord relational style, and look to some of the nosql databases for a solution. They find Mongo, a document database, and feel it might provide the flexibility they need. After a bit of research they pick out a persistence library which seems popular and well maintained. It even emulates most of ActiveRecord's behaviour, style and relational philosophy. Great!
@@ -131,7 +135,7 @@ Future development
131
135
  1. Migrations (e.g. when you rename classes or modify their collection style).
132
136
  2. Some kind of validations, perhaps.
133
137
 
134
-
138
+ [lm_rails]:http://github.com/elliotcm/light_mongo-rails
135
139
 
136
140
 
137
141
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.3.0
1
+ 0.4.0
@@ -6,4 +6,4 @@ def db_teardown
6
6
  after(:each) do
7
7
  LightMongo.connection.drop_database(LightMongo.database.name)
8
8
  end
9
- end
9
+ end
@@ -89,7 +89,9 @@ module LightMongo
89
89
  end
90
90
 
91
91
  def find(query=nil)
92
- query = {'_id' => query} unless query.nil? or query.is_a?(Hash)
92
+ return new(collection.find_one(query)) if query.is_a?(Mongo::ObjectID)
93
+ return new(collection.find_one(Mongo::ObjectID.from_string(query))) if query.is_a? String
94
+
93
95
  collection.find(query).map{|bson_hash| new(bson_hash)}
94
96
  end
95
97
 
@@ -27,7 +27,7 @@ module LightMongo
27
27
  end
28
28
 
29
29
  if object_to_deserialize.has_key?('_embed') and object_to_deserialize['_embed'] == true
30
- return Object.const_get(class_name).find(object_to_deserialize['_id']).first
30
+ return Object.const_get(class_name).find(object_to_deserialize['_id'])
31
31
  end
32
32
  end
33
33
 
@@ -160,19 +160,41 @@ describe LightMongo::Document::Persistence do
160
160
  end
161
161
 
162
162
  describe ".find(query=nil)" do
163
- before(:each) do
164
- @no_query_results = mock(:no_query_results)
165
- @query_results = mock(:query_results)
166
- TestClass.stub!(:new).with(@no_query_results).and_return(@no_query_object = mock(:no_query_object))
167
- TestClass.stub!(:new).with(@query_results).and_return(@query_object = mock(:query_object))
163
+ context "when passed a string" do
164
+ before(:each) do
165
+ @query = "4bae6ac37bc7693598000001"
166
+ Mongo::ObjectID.stub!(:from_string).with(@query).and_return(oid = mock(:oid))
167
+ @test_class_collection.stub!(:find_one).with(oid).and_return(results = mock(:results))
168
+ TestClass.stub!(:new).with(results).and_return(@TestObject = mock(:TestObject))
169
+ end
170
+
171
+ it "does a single-record lookup using the string as an ID" do
172
+ TestClass.find(@query).should == @TestObject
173
+ end
174
+ end
175
+
176
+ context "when passed an OID" do
177
+ before(:each) do
178
+ @query = Mongo::ObjectID.from_string("4bae6ac37bc7693598000001")
179
+ @test_class_collection.stub!(:find_one).with(@query).and_return(results = mock(:results))
180
+ TestClass.stub!(:new).with(results).and_return(@TestObject = mock(:TestObject))
181
+ end
182
+
183
+ it "does a single-record lookup using the ID" do
184
+ TestClass.find(@query).should == @TestObject
185
+ end
168
186
  end
169
187
 
170
- it "maps MongoDB::Collection.find" do
171
- @test_class_collection.should_receive(:find).and_return([@no_query_results])
172
- TestClass.find.should == [@no_query_object]
188
+ context "when passed anything else" do
189
+ before(:each) do
190
+ @query = mock(:query)
191
+ @test_class_collection.stub!(:find).with(@query).and_return([results = mock(:results)])
192
+ TestClass.stub!(:new).with(results).and_return(@TestObject = mock(:TestObject))
193
+ end
173
194
 
174
- @test_class_collection.should_receive(:find).with({'_id' => query = mock(:query)}).and_return([@query_results])
175
- TestClass.find(query).should == [@query_object]
195
+ it "does a multi-record lookup on the query" do
196
+ TestClass.find(@query).should == [@TestObject]
197
+ end
176
198
  end
177
199
  end
178
200
 
@@ -97,7 +97,7 @@ describe Serializer do
97
97
  end
98
98
 
99
99
  it "recovers the linked document" do
100
- TestClass.stub!(:find).with(@id).and_return([test_instance = mock(:test_instance)])
100
+ TestClass.stub!(:find).with(@id).and_return(test_instance = mock(:test_instance))
101
101
 
102
102
  Serializer.deserialize(@object).should == test_instance
103
103
  end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.4.0
10
10
  platform: ruby
11
11
  authors:
12
12
  - Elliot Crosby-McCullough
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-03-27 00:00:00 +00:00
17
+ date: 2010-03-28 00:00:00 +00:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency