lou 0.2.1 → 0.2.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/README.md +3 -3
- data/lib/lou/transformer.rb +4 -2
- data/lib/lou/version.rb +1 -1
- data/spec/lou/transformer_spec.rb +33 -8
- 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: 4dea25041b0ad0a9ec2e1bf43bcceff1c300d9b6
|
4
|
+
data.tar.gz: 92d368f757c66030610c3b6492d3773456dc384f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3456df775aada99d3f62fe854e65f92a230c67a6ca0d3af2c526da35244b7892722018118f51ac0319505a5db4fa802e6cdcad0d2fa701af6ce4881d41bd80c4
|
7
|
+
data.tar.gz: 53cfccb9aa2ef91cd5b1f64a56ad7cb5cd19888d40d0190d61418db1207dd993e28e17250cea5fb9625b67d00fbb0f3490b871affa89eaccfa62b03d46230f73
|
data/README.md
CHANGED
@@ -44,13 +44,13 @@ original = HashTransformer.reverse(result)
|
|
44
44
|
# {:an_old_key=>"this is old"}
|
45
45
|
~~~
|
46
46
|
|
47
|
-
The steps are applied in the order that they're defined, when the
|
47
|
+
The steps are applied in the order that they're defined, when the `apply` method is called, with each step receiving the result of the previous one. The process can be reversed using the `reverse` method. Note that for each step, the input is the result of the previous step.
|
48
48
|
|
49
|
-
If
|
49
|
+
If `reverse_on` is defined, then any completed steps will be reversed if the exception specified is raised.
|
50
50
|
|
51
51
|
Transformers inherit the steps of their parent class, so it's possible to reuse steps by using inheritance.
|
52
52
|
|
53
53
|
Credits
|
54
54
|
-------
|
55
55
|
|
56
|
-
Lou
|
56
|
+
Lou was originally inspired by [Hash Mapper](http://github.com/ismasan) by [Ismael Celis](http://github.com/ismasan) to be a way of transforming hashes, however, it evolved into a general purpose pipeline for arbitrary blocks of code.
|
data/lib/lou/transformer.rb
CHANGED
@@ -33,7 +33,8 @@ module Lou
|
|
33
33
|
applied_steps += 1
|
34
34
|
end
|
35
35
|
rescue error_class => e
|
36
|
-
|
36
|
+
reverse(input, applied_steps) if total_steps == steps.count
|
37
|
+
raise e
|
37
38
|
end
|
38
39
|
input
|
39
40
|
end
|
@@ -46,7 +47,8 @@ module Lou
|
|
46
47
|
reversed_steps += 1
|
47
48
|
end
|
48
49
|
rescue error_class => e
|
49
|
-
|
50
|
+
apply(output, reversed_steps) if total_steps == steps.count
|
51
|
+
raise e
|
50
52
|
end
|
51
53
|
output
|
52
54
|
end
|
data/lib/lou/version.rb
CHANGED
@@ -133,12 +133,14 @@ module Lou
|
|
133
133
|
end
|
134
134
|
|
135
135
|
context 'when #reverse_on has been set' do
|
136
|
+
let!(:error_class) do
|
137
|
+
class SpecialError < StandardError; end
|
138
|
+
end
|
139
|
+
|
136
140
|
let(:parent) do
|
137
141
|
Class.new do
|
138
142
|
extend Lou::Transformer
|
139
143
|
|
140
|
-
class SpecialError < StandardError; end
|
141
|
-
|
142
144
|
reverse_on SpecialError
|
143
145
|
end
|
144
146
|
end
|
@@ -157,7 +159,7 @@ module Lou
|
|
157
159
|
it 'reverses no steps when the specified error is raised' do
|
158
160
|
expect(target).to_not receive(:create)
|
159
161
|
expect(target).to_not receive(:destroy)
|
160
|
-
klass.apply(target)
|
162
|
+
expect { klass.apply(target) }.to raise_error(SpecialError)
|
161
163
|
end
|
162
164
|
end
|
163
165
|
|
@@ -165,7 +167,7 @@ module Lou
|
|
165
167
|
it 'applies no steps when the specified error is raised' do
|
166
168
|
expect(target).to_not receive(:destroy)
|
167
169
|
expect(target).to_not receive(:create)
|
168
|
-
klass.reverse(target)
|
170
|
+
expect { klass.reverse(target) }.to raise_error(SpecialError)
|
169
171
|
end
|
170
172
|
end
|
171
173
|
end
|
@@ -182,18 +184,41 @@ module Lou
|
|
182
184
|
let(:target) { instance_double('Target') }
|
183
185
|
|
184
186
|
describe '#apply' do
|
185
|
-
it 'reverses all successfully applied steps when the specified error is raised' do
|
187
|
+
it 'reverses all successfully applied steps before raising the error when the specified error is raised' do
|
186
188
|
expect(target).to receive(:create).once.with(1).ordered
|
187
189
|
expect(target).to receive(:destroy).once.with(1).ordered
|
188
|
-
klass.apply(target)
|
190
|
+
expect { klass.apply(target) }.to raise_error(SpecialError)
|
189
191
|
end
|
190
192
|
end
|
191
193
|
|
192
194
|
describe '#reverse' do
|
193
|
-
it 'reapplies all successfully reversed steps when the specified error is raised' do
|
195
|
+
it 'reapplies all successfully reversed steps before raising the error when the specified error is raised' do
|
194
196
|
expect(target).to receive(:destroy).once.with(3).ordered
|
195
197
|
expect(target).to receive(:create).once.with(3).ordered
|
196
|
-
klass.reverse(target)
|
198
|
+
expect { klass.reverse(target) }.to raise_error(SpecialError)
|
199
|
+
end
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
context 'and the up and down steps should lead to an infinite loop' do
|
204
|
+
let(:klass) do
|
205
|
+
Class.new(parent) do
|
206
|
+
step.up { |x| x.create(1); x }.down { |_| fail SpecialError, 'fail on down' }
|
207
|
+
step.up { |_| fail SpecialError, 'fail on up' }.down { |x| x.destroy(2); x }
|
208
|
+
end
|
209
|
+
end
|
210
|
+
|
211
|
+
describe '#apply' do
|
212
|
+
it 'raises the error from the down step' do
|
213
|
+
expect(target).to receive(:create).once.with(1)
|
214
|
+
expect { klass.apply(target) }.to raise_error(SpecialError, 'fail on down')
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
describe '#reverse' do
|
219
|
+
it 'raises the error from the up step' do
|
220
|
+
expect(target).to receive(:destroy).once.with(2)
|
221
|
+
expect { klass.reverse(target) }.to raise_error(SpecialError, 'fail on up')
|
197
222
|
end
|
198
223
|
end
|
199
224
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lou
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Iain Beeston
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-08-
|
11
|
+
date: 2014-08-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|