mingo 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -10,13 +10,13 @@ class Mingo
10
10
  !!db
11
11
  end
12
12
 
13
- def connect(dbname_or_uri)
13
+ def connect(dbname_or_uri, options = {})
14
14
  self.collection = nil
15
15
  self.db = if dbname_or_uri.index('mongodb://') == 0
16
- connection = Mongo::Connection.from_uri(dbname_or_uri)
16
+ connection = Mongo::Connection.from_uri(dbname_or_uri, options)
17
17
  connection.db(connection.auths.last['db_name'])
18
18
  else
19
- Mongo::Connection.new.db(dbname_or_uri)
19
+ Mongo::Connection.new(nil, nil, options).db(dbname_or_uri)
20
20
  end
21
21
  end
22
22
 
data/lib/mingo/cursor.rb CHANGED
@@ -70,7 +70,7 @@ class Mingo
70
70
  if @limit > 0 || @skip > 0
71
71
  ids = selector[:_id]["$in"]
72
72
  old_skip = @skip
73
- selector[:_id]["$in"] = ids[@skip, @limit > 0 ? @limit : ids.size]
73
+ selector[:_id]["$in"] = Array(ids[@skip, @limit > 0 ? @limit : ids.size])
74
74
  @skip = 0
75
75
  begin
76
76
  yield
@@ -82,6 +82,11 @@ class Mingo
82
82
  def loaded?
83
83
  !!@loaded
84
84
  end
85
+
86
+ def reload
87
+ @loaded = @join_loaded = nil
88
+ self
89
+ end
85
90
 
86
91
  def find(selector = {}, options = {}, &block)
87
92
  @model.find(selector, find_options.merge(options), &block)
@@ -90,7 +95,17 @@ class Mingo
90
95
  def respond_to?(method, priv = false)
91
96
  super || method_missing(:respond_to?, method, priv)
92
97
  end
93
-
98
+
99
+ def reset_counter_cache
100
+ delta = join_cursor.count - counter_cache
101
+ change_counter_cache delta
102
+ counter_cache
103
+ end
104
+
105
+ def cache_key
106
+ [@parent, @property, counter_cache]
107
+ end
108
+
94
109
  private
95
110
 
96
111
  def method_missing(method, *args, &block)
data/lib/mingo.rb CHANGED
@@ -54,4 +54,15 @@ class Mingo < Hash
54
54
  def ==(other)
55
55
  other.is_a?(self.class) and other.id == self.id
56
56
  end
57
+
58
+ def cache_key
59
+ model_key = self.class.model_name.cache_key
60
+ if not persisted?
61
+ "#{model_key}/new"
62
+ elsif timestamp = self['updated_at']
63
+ "#{model_key}/#{id}-#{timestamp.utc.strftime("%Y%m%d%H%M%S")}"
64
+ else
65
+ "#{model_key}/#{id}"
66
+ end
67
+ end
57
68
  end
data/spec/mingo_spec.rb CHANGED
@@ -200,13 +200,44 @@ describe User do
200
200
  cursor.to_a.should == [@doc2]
201
201
  end
202
202
 
203
+ it "doesn't die when offset is out of bounds" do
204
+ cursor = described_class.find([@doc3.id, @doc1.id, @doc2.id]).skip(4)
205
+ cursor.to_a.should be_empty
206
+ end
207
+
203
208
  it "returns correct count" do
204
209
  cursor = described_class.find([@doc3.id, @doc1.id, @doc2.id]).limit(1).skip(2)
205
- cursor.next_document
206
210
  cursor.count.should == 3
207
211
  end
212
+
213
+ it "works with empty ID list" do
214
+ results = described_class.find([]).to_a
215
+ results.should be_empty
216
+ end
217
+
218
+ it "works with nonexistent ID" do
219
+ results = described_class.find([BSON::ObjectId.new]).to_a
220
+ results.should be_empty
221
+ end
208
222
  end
209
-
223
+
224
+ context "cache_key" do
225
+ it "has it" do
226
+ build.cache_key.should == "users/new"
227
+ end
228
+
229
+ it "includes the ID if persisted" do
230
+ user = create
231
+ user.cache_key.should == "users/#{user.id}"
232
+ end
233
+
234
+ it "includes the timestamp if present" do
235
+ user = create
236
+ user['updated_at'] = Time.utc_time 2011, 12, 13, 14, 15, 16
237
+ user.cache_key.should == "users/#{user.id}-20111213141516"
238
+ end
239
+ end
240
+
210
241
  def build(*args)
211
242
  described_class.new(*args)
212
243
  end
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: mingo
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.4.1
5
+ version: 0.4.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - "Mislav Marohni\xC4\x87"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-08-31 00:00:00 Z
13
+ date: 2011-09-20 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: mongo