waterfall 1.0.6 → 1.1.0
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/Gemfile.lock +2 -2
- data/README.md +25 -3
- data/changelog.md +4 -0
- data/lib/waterfall.rb +4 -5
- data/lib/waterfall/version.rb +1 -1
- data/spec/wf_object_spec.rb +24 -3
- 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: 27aca92eae9decca2d3a32be3293985de0b219e1
|
4
|
+
data.tar.gz: d9e95c5a4a7aa9438f06bd67337bd5a87e7c8dd8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e178dab7e10f42a2215c17cd991d3af286a07908bd2e4c3d4f8f3b8769ee576a8c9e455af3a57c037a866bd3693aecf940894ef601f4684f3b4bbfeb15873f73
|
7
|
+
data.tar.gz: eac428e1c45e83e652451bf37fa972bbe03db7ad90c61c47a8597d7b512852d33c48d5010531fc78b52b006a9dc8bee71dd5d5b80f8931c7e9138f18addbd2de
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-

|
2
1
|
[](https://codeclimate.com/github/apneadiving/waterfall)
|
3
2
|
[](https://codeclimate.com/github/apneadiving/waterfall/coverage)
|
4
3
|
[](https://travis-ci.org/apneadiving/waterfall)
|
@@ -9,7 +8,14 @@ Chain ruby commands, and treat them like a flow, which provides a new approach t
|
|
9
8
|
|
10
9
|
When logic is complicated, waterfalls show their true power and let you write intention revealing code. Above all they excel at chaining services.
|
11
10
|
|
12
|
-
|
11
|
+
#### Material
|
12
|
+
<a href="https://leanpub.com/the-unhappy-path"> <img align="left" width="80" height="116" src="https://apneadiving.github.io/images/unhappy-path.png"> </a>
|
13
|
+
Upcoming book about failure management patterns, leveraging the gem: [The Unhappy path](https://leanpub.com/the-unhappy-path)
|
14
|
+
|
15
|
+
General presentation blog post there: [Chain services objects like a boss](https://medium.com/p/chain-service-objects-like-a-boss-35d0b83606ab).
|
16
|
+
|
17
|
+
Reach me [@apneadiving](https://twitter.com/apneadiving)
|
18
|
+
|
13
19
|
|
14
20
|
#### Overview
|
15
21
|
|
@@ -19,7 +25,7 @@ Here is a basic representation:
|
|
19
25
|
- green, the flow goes on, `chain` by `chain`
|
20
26
|
- red its bypassed and only `on_dam` blocks are executed.
|
21
27
|
|
22
|
-

|
23
29
|
|
24
30
|
#### Example
|
25
31
|
|
@@ -176,6 +182,18 @@ else
|
|
176
182
|
self.chain { Service3.new }
|
177
183
|
end
|
178
184
|
```
|
185
|
+
### Halting chain
|
186
|
+
Sometimes you have a flow and you need a return value. You can use `halt_chain`, which is executed whether or not the flow is dammed. It returns what the block returns. As a consequence, it cannot be chained anymore, so it must be the last command:
|
187
|
+
|
188
|
+
```ruby
|
189
|
+
self.halt_chain do |outflow, error_pool|
|
190
|
+
if error_pool
|
191
|
+
# what you want to return on error
|
192
|
+
else
|
193
|
+
# what you want to return from the outflow
|
194
|
+
end
|
195
|
+
end
|
196
|
+
```
|
179
197
|
|
180
198
|
### Rails and transactions
|
181
199
|
I'm used to wrap every single object involving database interactions within transactions, so it can be rolled back on error.
|
@@ -224,6 +242,10 @@ end
|
|
224
242
|
```
|
225
243
|
The huge benefit is that if you call services from services, everything will be rolled back.
|
226
244
|
|
245
|
+
### FYI
|
246
|
+
|
247
|
+
`Flow` is just an alias for the `Wf` class, so just use the one you prefer :)
|
248
|
+
|
227
249
|
Examples / Presentations
|
228
250
|
========================
|
229
251
|
- Check the [wiki for other examples](https://github.com/apneadiving/waterfall/wiki/Refactoring-examples).
|
data/changelog.md
CHANGED
data/lib/waterfall.rb
CHANGED
@@ -33,11 +33,6 @@ module Waterfall
|
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
|
-
def chain_wf(mapping_hash = nil, &block)
|
37
|
-
warn "[DEPRECATION] `chain_wf` is deprecated. Please use `chain` instead."
|
38
|
-
chain(mapping_hash, &block)
|
39
|
-
end
|
40
|
-
|
41
36
|
def on_dam(&block)
|
42
37
|
::Waterfall::OnDam
|
43
38
|
.new(self)
|
@@ -55,6 +50,10 @@ module Waterfall
|
|
55
50
|
self
|
56
51
|
end
|
57
52
|
|
53
|
+
def halt_chain(&block)
|
54
|
+
yield(outflow, error_pool)
|
55
|
+
end
|
56
|
+
|
58
57
|
def dammed?
|
59
58
|
!error_pool.nil?
|
60
59
|
end
|
data/lib/waterfall/version.rb
CHANGED
data/spec/wf_object_spec.rb
CHANGED
@@ -27,6 +27,11 @@ describe Flow do
|
|
27
27
|
expect(wf.outflow.foo).to eq 1
|
28
28
|
end
|
29
29
|
|
30
|
+
it 'returns itself to enable chaining' do
|
31
|
+
expect(wf.chain{ }).to eq wf
|
32
|
+
expect(wf.on_dam{ }).to eq wf
|
33
|
+
end
|
34
|
+
|
30
35
|
it 'chain yields outflow and waterfall itself' do
|
31
36
|
wf.chain do |outflow, waterfall|
|
32
37
|
expect(outflow).to eq wf.outflow
|
@@ -98,8 +103,24 @@ describe Flow do
|
|
98
103
|
expect { wf.chain(:foo) { Flow.new } }.to raise_error(Waterfall::IncorrectChainingArgumentError, Waterfall::Chain::MAPPING_ERROR_MESSAGE)
|
99
104
|
end
|
100
105
|
|
101
|
-
|
102
|
-
|
103
|
-
|
106
|
+
describe 'halt_chain' do
|
107
|
+
it "yields expected values" do
|
108
|
+
wf.chain(:foo) { 1 }.halt_chain do |outflow, error_pool|
|
109
|
+
expect(outflow).to eq wf.outflow
|
110
|
+
expect(error_pool).to eq wf.error_pool
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
it "yields expected values even if dammed" do
|
115
|
+
wf.chain(:foo) { 1 }.dam("errr").halt_chain do |outflow, error_pool|
|
116
|
+
expect(outflow).to eq wf.outflow
|
117
|
+
expect(error_pool).to eq wf.error_pool
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
121
|
+
it "returns what the block returns" do
|
122
|
+
expect(wf.halt_chain { "return value" }).to eq "return value"
|
123
|
+
end
|
124
|
+
|
104
125
|
end
|
105
126
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: waterfall
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Benjamin Roth
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-08-
|
11
|
+
date: 2017-08-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|