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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: fd8b128c7302c3b25ec0274ce6fea18599e8acd2
4
- data.tar.gz: bb2a8e05fee133b6dd322c5fd9cdc3140857a7ed
3
+ metadata.gz: 281b8113071be47cb1c600fcca69c0ea22e76227
4
+ data.tar.gz: d893f5a805c2f702526e7b98eae2df9c9598d97a
5
5
  SHA512:
6
- metadata.gz: a578bbc736a87c373b440fd32750d03791e5fcad33b69f6331602d3a4cb1ac9c31473bea8f2d22dfdf7812a71dd4a29b7a280a531e52c138684ef4d9f8a6bf2d
7
- data.tar.gz: cb5f2f74d06f4c4a157cee9aba307b2f6516855611f4541e0e95a1836bb210486bc73e4e7504cb9f0977497900781deb3d91cf391be2206d34e42347a08b77d8
6
+ metadata.gz: 426b3c2c00bb3bf7207e80fe99a75c72c6327da3ac26abe1c417b030caea719f736eb71933be35dfe27656031bfa74838550fdcee9f74370feba7fa755cc1aba
7
+ data.tar.gz: 99014ea40caec7e4bbf74f4d74e77d637409556e469a80c10a835709e62fe07f040ddb2d227e16fb7471ebf299f6f67ee4ffee52981ee773aa608e389d429452
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- toughguy (0.1.0)
4
+ toughguy (0.2.0)
5
5
  assistance (= 0.1.5)
6
6
  bson_ext (= 1.11.1)
7
7
  mongo (= 1.11.1)
@@ -8,7 +8,7 @@ module ToughGuy
8
8
  end
9
9
 
10
10
  def set(hash)
11
- collection.update({_id: id}, '$set' => hash)
11
+ model.update({_id: id}, '$set' => hash)
12
12
  @values.merge!(hash)
13
13
  end
14
14
 
@@ -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
- collection.update({_id: id}, values.exclude(:_id))
67
+ self.class.update({_id: id}, values.exclude(:_id))
64
68
  end
65
69
  end
66
-
70
+
67
71
  def update(hash)
68
- collection.update({_id: id}, hash)
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
- collection.update(*args)
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
@@ -1,4 +1,4 @@
1
1
  # encoding: UTF-8
2
2
  module ToughGuy
3
- Version = '0.1.0'
3
+ Version = '0.2.0'
4
4
  end
@@ -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.1.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-15 00:00:00.000000000 Z
11
+ date: 2014-12-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mongo