toughguy 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/toughguy/mapped_model.rb +1 -1
- data/lib/toughguy/model.rb +24 -7
- data/lib/toughguy/version.rb +1 -1
- data/spec/toughguy_spec.rb +16 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 281b8113071be47cb1c600fcca69c0ea22e76227
|
4
|
+
data.tar.gz: d893f5a805c2f702526e7b98eae2df9c9598d97a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 426b3c2c00bb3bf7207e80fe99a75c72c6327da3ac26abe1c417b030caea719f736eb71933be35dfe27656031bfa74838550fdcee9f74370feba7fa755cc1aba
|
7
|
+
data.tar.gz: 99014ea40caec7e4bbf74f4d74e77d637409556e469a80c10a835709e62fe07f040ddb2d227e16fb7471ebf299f6f67ee4ffee52981ee773aa608e389d429452
|
data/Gemfile.lock
CHANGED
data/lib/toughguy/model.rb
CHANGED
@@ -31,6 +31,10 @@ module ToughGuy
|
|
31
31
|
def model
|
32
32
|
self.class
|
33
33
|
end
|
34
|
+
|
35
|
+
def bulk(&block)
|
36
|
+
model.bulk(&block)
|
37
|
+
end
|
34
38
|
|
35
39
|
def [](k)
|
36
40
|
@values[k]
|
@@ -60,16 +64,16 @@ module ToughGuy
|
|
60
64
|
values[:_id] = collection.insert(values)
|
61
65
|
after_create
|
62
66
|
else
|
63
|
-
|
67
|
+
self.class.update({_id: id}, values.exclude(:_id))
|
64
68
|
end
|
65
69
|
end
|
66
|
-
|
70
|
+
|
67
71
|
def update(hash)
|
68
|
-
|
72
|
+
model.update({_id: id}, hash)
|
69
73
|
end
|
70
74
|
|
71
75
|
def set(hash)
|
72
|
-
update('$set' => hash)
|
76
|
+
model.update({_id: id}, '$set' => hash)
|
73
77
|
@values.merge!(hash.symbolize_keys)
|
74
78
|
end
|
75
79
|
|
@@ -115,8 +119,16 @@ module ToughGuy
|
|
115
119
|
def exists?
|
116
120
|
model.find(id) != nil
|
117
121
|
end
|
118
|
-
|
122
|
+
|
119
123
|
class << self
|
124
|
+
def bulk(&block)
|
125
|
+
@bulk = collection.initialize_ordered_bulk_op
|
126
|
+
block.call
|
127
|
+
@bulk.execute
|
128
|
+
ensure
|
129
|
+
@bulk = nil
|
130
|
+
end
|
131
|
+
|
120
132
|
def add_index(spec, opts={})
|
121
133
|
@indexes ||= []
|
122
134
|
@indexes << [spec, opts]
|
@@ -242,9 +254,14 @@ module ToughGuy
|
|
242
254
|
end
|
243
255
|
|
244
256
|
def update(*args)
|
245
|
-
|
257
|
+
if @bulk
|
258
|
+
@bulk.find(args[0]).update(args[1])
|
259
|
+
else
|
260
|
+
collection.update(*args)
|
261
|
+
end
|
262
|
+
# (@bulk || collection).update(*args)
|
246
263
|
end
|
247
|
-
|
264
|
+
|
248
265
|
def stats
|
249
266
|
collection.stats
|
250
267
|
end
|
data/lib/toughguy/version.rb
CHANGED
data/spec/toughguy_spec.rb
CHANGED
@@ -16,6 +16,22 @@ describe "Model#update" do
|
|
16
16
|
m.refresh
|
17
17
|
m.values.should == {c: 3, d: 4, _id: id}
|
18
18
|
end
|
19
|
+
|
20
|
+
it "should work when doing bulk updates" do
|
21
|
+
m1 = Mau.create(a: 1, b: 2)
|
22
|
+
m2 = Mau.create(a: 3, b: 4)
|
23
|
+
|
24
|
+
m1.bulk do
|
25
|
+
m1.set(a: 5, c: 6)
|
26
|
+
m2.set(b: 7, c: 8)
|
27
|
+
end
|
28
|
+
|
29
|
+
m1.refresh
|
30
|
+
m2.refresh
|
31
|
+
|
32
|
+
m1.values.should == {a: 5, b: 2, c: 6, _id: m1.id}
|
33
|
+
m2.values.should == {a: 3, b: 7, c: 8, _id: m2.id}
|
34
|
+
end
|
19
35
|
end
|
20
36
|
|
21
37
|
describe "Model#set" do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: toughguy
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sharon Rosner
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-12-
|
11
|
+
date: 2014-12-25 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mongo
|