restruct 0.0.1 → 0.0.2
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.
- checksums.yaml +4 -4
- data/lib/restruct.rb +1 -0
- data/lib/restruct/batch.rb +38 -0
- data/lib/restruct/version.rb +1 -1
- data/spec/batch_spec.rb +53 -0
- metadata +5 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 323a7886178c57fee15bdad49f5741213f0eb240
|
4
|
+
data.tar.gz: 70061f00219a2599c15bdf3c0ed47bac4a447f15
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6d056c3d518860fe8f0a823a7994406ab592ef4a36b24f31c15539fd144090ab08e56c13d8b90a89f6f21b4da813a75f0a2f584b5679a5cd2cba69edfbf3cdde
|
7
|
+
data.tar.gz: 06c08f25b4c01df0b510f2a2f0afb460027d6a779424cac5838f4941fc1ef203a6ad6f3cfaf48cc9d8fa3abcd640df4d112656a18a432b1bcb77a07a79e2f592
|
data/lib/restruct.rb
CHANGED
@@ -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
|
data/lib/restruct/version.rb
CHANGED
data/spec/batch_spec.rb
ADDED
@@ -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.
|
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-
|
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
|