mingo 0.1.0 → 0.1.1
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/lib/mingo/cursor.rb +4 -0
- data/lib/mingo/many_proxy.rb +12 -0
- data/lib/mingo.rb +64 -11
- metadata +4 -4
data/lib/mingo/cursor.rb
CHANGED
data/lib/mingo/many_proxy.rb
CHANGED
@@ -48,6 +48,10 @@ class Mingo
|
|
48
48
|
def object_ids
|
49
49
|
@embedded
|
50
50
|
end
|
51
|
+
|
52
|
+
def include?(doc)
|
53
|
+
object_ids.include? convert(doc)
|
54
|
+
end
|
51
55
|
|
52
56
|
def convert(doc)
|
53
57
|
doc.id
|
@@ -67,6 +71,14 @@ class Mingo
|
|
67
71
|
unload_collection
|
68
72
|
@embedded.delete doc
|
69
73
|
end
|
74
|
+
|
75
|
+
def loaded?
|
76
|
+
!!@collection
|
77
|
+
end
|
78
|
+
|
79
|
+
def respond_to?(method, priv = false)
|
80
|
+
super || method_missing(:respond_to?, method, priv)
|
81
|
+
end
|
70
82
|
|
71
83
|
private
|
72
84
|
|
data/lib/mingo.rb
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
require 'active_support/core_ext/hash/conversions'
|
1
2
|
require 'mongo'
|
2
3
|
require 'active_model'
|
3
4
|
require 'hashie/dash'
|
@@ -6,6 +7,10 @@ BSON::ObjectId.class_eval do
|
|
6
7
|
def self.[](id)
|
7
8
|
self === id ? id : from_string(id)
|
8
9
|
end
|
10
|
+
|
11
|
+
def id
|
12
|
+
self
|
13
|
+
end
|
9
14
|
end
|
10
15
|
|
11
16
|
class Mingo < Hashie::Dash
|
@@ -72,7 +77,7 @@ class Mingo < Hashie::Dash
|
|
72
77
|
attr_reader :changes
|
73
78
|
|
74
79
|
def initialize(obj = nil)
|
75
|
-
@changes =
|
80
|
+
@changes = {}
|
76
81
|
@destroyed = false
|
77
82
|
|
78
83
|
if obj and obj['_id'].is_a? BSON::ObjectId
|
@@ -93,6 +98,10 @@ class Mingo < Hashie::Dash
|
|
93
98
|
_regular_writer(property.to_s, value)
|
94
99
|
end
|
95
100
|
|
101
|
+
# keys are already strings
|
102
|
+
def stringify_keys() self end
|
103
|
+
alias :stringify_keys! :stringify_keys
|
104
|
+
|
96
105
|
def id
|
97
106
|
self['_id']
|
98
107
|
end
|
@@ -148,10 +157,17 @@ class Mingo < Hashie::Dash
|
|
148
157
|
end
|
149
158
|
|
150
159
|
def _regular_writer(key, value)
|
151
|
-
|
152
|
-
changes[key.to_sym][1] = value unless value == old_value
|
160
|
+
track_change(key, value)
|
153
161
|
super
|
154
162
|
end
|
163
|
+
|
164
|
+
def track_change(key, value)
|
165
|
+
old_value = _regular_reader(key)
|
166
|
+
unless value == old_value
|
167
|
+
memo = (changes[key.to_sym] ||= [old_value])
|
168
|
+
memo[0] == value ? changes.delete(key.to_sym) : (memo[1] = value)
|
169
|
+
end
|
170
|
+
end
|
155
171
|
end
|
156
172
|
|
157
173
|
if $0 == __FILE__
|
@@ -169,9 +185,18 @@ if $0 == __FILE__
|
|
169
185
|
User.collection.remove
|
170
186
|
end
|
171
187
|
|
188
|
+
it "obtains an ID by saving" do
|
189
|
+
user = build :name => 'Mislav'
|
190
|
+
user.should_not be_persisted
|
191
|
+
user.id.should be_nil
|
192
|
+
user.save
|
193
|
+
raw_doc(user.id)['name'].should == 'Mislav'
|
194
|
+
user.should be_persisted
|
195
|
+
user.id.should be_a(BSON::ObjectId)
|
196
|
+
end
|
197
|
+
|
172
198
|
it "tracks changes attribute" do
|
173
199
|
user = build
|
174
|
-
user.should_not be_persisted
|
175
200
|
user.should_not be_changed
|
176
201
|
user.name = 'Mislav'
|
177
202
|
user.should be_changed
|
@@ -179,9 +204,15 @@ if $0 == __FILE__
|
|
179
204
|
user.name = 'Mislav2'
|
180
205
|
user.changes[:name].should == [nil, 'Mislav2']
|
181
206
|
user.save
|
182
|
-
user.should be_persisted
|
183
207
|
user.should_not be_changed
|
184
|
-
|
208
|
+
end
|
209
|
+
|
210
|
+
it "forgets changed attribute when reset to original value" do
|
211
|
+
user = create :name => 'Mislav'
|
212
|
+
user.name = 'Mislav2'
|
213
|
+
user.should be_changed
|
214
|
+
user.name = 'Mislav'
|
215
|
+
user.should_not be_changed
|
185
216
|
end
|
186
217
|
|
187
218
|
it "has a human model name" do
|
@@ -218,11 +249,33 @@ if $0 == __FILE__
|
|
218
249
|
end
|
219
250
|
end
|
220
251
|
|
221
|
-
|
222
|
-
|
223
|
-
|
224
|
-
|
225
|
-
|
252
|
+
context "existing doc" do
|
253
|
+
before do
|
254
|
+
@id = described_class.collection.insert :name => 'Mislav', :age => 26
|
255
|
+
end
|
256
|
+
|
257
|
+
it "finds a doc by string ID" do
|
258
|
+
user = described_class.first(@id.to_s)
|
259
|
+
user.id.should == @id
|
260
|
+
user.name.should == 'Mislav'
|
261
|
+
user.age.should == 26
|
262
|
+
end
|
263
|
+
|
264
|
+
it "is unchanged after loading" do
|
265
|
+
user = described_class.first(@id)
|
266
|
+
user.should_not be_changed
|
267
|
+
user.age = 27
|
268
|
+
user.should be_changed
|
269
|
+
user.changes.keys.should == [:age]
|
270
|
+
end
|
271
|
+
|
272
|
+
it "doesn't get changed by an inspect" do
|
273
|
+
user = described_class.first(@id)
|
274
|
+
# triggers AS stringify_keys, which dups the Dash and writes to it,
|
275
|
+
# which mutates the @changes hash from the original Dash
|
276
|
+
user.inspect
|
277
|
+
user.should_not be_changed
|
278
|
+
end
|
226
279
|
end
|
227
280
|
|
228
281
|
it "returns nil for non-existing doc" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mingo
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 25
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 1
|
9
|
-
-
|
10
|
-
version: 0.1.
|
9
|
+
- 1
|
10
|
+
version: 0.1.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- "Mislav Marohni\xC4\x87"
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-09-
|
18
|
+
date: 2010-09-12 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|