restruct 0.0.1 → 0.0.2

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: 9a3ceeabf9eae0c207f69176f1f7933eb46068e6
4
- data.tar.gz: c52480abf0ad9fe754617cc4b8c845e7b8e16e27
3
+ metadata.gz: 323a7886178c57fee15bdad49f5741213f0eb240
4
+ data.tar.gz: 70061f00219a2599c15bdf3c0ed47bac4a447f15
5
5
  SHA512:
6
- metadata.gz: dc9c0deaa324df308c8725e754e6195222a0d3749d7d436333c36451e394cadb2bca408bfdc3c00f730287cb46c4b2d12bbc4a0b68c1f3f13ef267fadf645648
7
- data.tar.gz: 249265a027998c43c5ace27ec7f5bb1871960bae74ba73e50cd8a77ce4aa5c3dacea6b45d637060e99e8e672be11bf33da91f51e6aab8fa0228baeecfff05379
6
+ metadata.gz: 6d056c3d518860fe8f0a823a7994406ab592ef4a36b24f31c15539fd144090ab08e56c13d8b90a89f6f21b4da813a75f0a2f584b5679a5cd2cba69edfbf3cdde
7
+ data.tar.gz: 06c08f25b4c01df0b510f2a2f0afb460027d6a779424cac5838f4941fc1ef203a6ad6f3cfaf48cc9d8fa3abcd640df4d112656a18a432b1bcb77a07a79e2f592
@@ -14,6 +14,7 @@ require_relative 'restruct/marshalizable'
14
14
  require_relative 'restruct/marshal_array'
15
15
  require_relative 'restruct/marshal_set'
16
16
  require_relative 'restruct/marshal_hash'
17
+ require_relative 'restruct/batch'
17
18
 
18
19
  module Restruct
19
20
 
@@ -0,0 +1,38 @@
1
+ module Restruct
2
+ class Batch
3
+ class << self
4
+
5
+ def execute(redis=nil)
6
+ redis ||= Restruct.redis
7
+ multi redis
8
+ yield
9
+ exec redis
10
+ rescue => ex
11
+ discard redis
12
+ raise ex
13
+ end
14
+
15
+ private
16
+
17
+ def nesting
18
+ @nesting ||= ::Hash.new { |h,k| h[k] = 0 }
19
+ end
20
+
21
+ def multi(redis)
22
+ redis.call 'MULTI' if nesting[redis] == 0
23
+ nesting[redis] += 1
24
+ end
25
+
26
+ def exec(redis)
27
+ nesting[redis] -= 1
28
+ redis.call 'EXEC' if nesting[redis] == 0
29
+ end
30
+
31
+ def discard(redis)
32
+ nesting[redis] -= 1
33
+ redis.call 'DISCARD' if nesting[redis] == 0
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -1,3 +1,3 @@
1
1
  module Restruct
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -0,0 +1,53 @@
1
+ require 'minitest_helper'
2
+
3
+ describe Restruct::Batch do
4
+
5
+ let(:connection) { Redic.new }
6
+
7
+ it 'Execute' do
8
+ hash = Restruct::Hash.new redis: connection
9
+
10
+ Restruct::Batch.execute do
11
+ h = Restruct::Hash.new id: hash.id
12
+ h.merge! a: 'x', b: 'y', c: 'z'
13
+
14
+ hash.must_be_empty
15
+ end
16
+
17
+ hash.to_h.must_equal 'a' => 'x', 'b' => 'y', 'c' => 'z'
18
+ end
19
+
20
+ it 'Discard' do
21
+ hash = Restruct::Hash.new redis: connection
22
+
23
+ proc do
24
+ Restruct::Batch.execute do
25
+ h = Restruct::Hash.new id: hash.id
26
+ h.merge! a: 'x', b: 'y', c: 'z'
27
+
28
+ raise 'Test error'
29
+ end
30
+ end.must_raise RuntimeError
31
+
32
+ hash.must_be_empty
33
+ end
34
+
35
+ it 'Nested' do
36
+ hash = Restruct::Hash.new redis: connection
37
+
38
+ Restruct::Batch.execute do
39
+ h = Restruct::Hash.new id: hash.id
40
+ h.merge! a: 'x', b: 'y'
41
+
42
+ Restruct::Batch.execute do
43
+ h[:c] = 'z'
44
+ hash.must_be_empty
45
+ end
46
+
47
+ hash.must_be_empty
48
+ end
49
+
50
+ hash.to_h.must_equal 'a' => 'x', 'b' => 'y', 'c' => 'z'
51
+ end
52
+
53
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: restruct
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gabriel Naiman
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-21 00:00:00.000000000 Z
11
+ date: 2015-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: redic
@@ -140,6 +140,7 @@ files:
140
140
  - Rakefile
141
141
  - lib/restruct.rb
142
142
  - lib/restruct/array.rb
143
+ - lib/restruct/batch.rb
143
144
  - lib/restruct/hash.rb
144
145
  - lib/restruct/id.rb
145
146
  - lib/restruct/marshal_array.rb
@@ -152,6 +153,7 @@ files:
152
153
  - lib/restruct/version.rb
153
154
  - restruct.gemspec
154
155
  - spec/array_spec.rb
156
+ - spec/batch_spec.rb
155
157
  - spec/coverage_helper.rb
156
158
  - spec/hash_spec.rb
157
159
  - spec/id_spec.rb
@@ -184,6 +186,7 @@ specification_version: 4
184
186
  summary: Redis structures
185
187
  test_files:
186
188
  - spec/array_spec.rb
189
+ - spec/batch_spec.rb
187
190
  - spec/coverage_helper.rb
188
191
  - spec/hash_spec.rb
189
192
  - spec/id_spec.rb