deterministic 0.8.0 → 0.8.1
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 +10 -0
- data/lib/deterministic/either/chain.rb +9 -0
- data/lib/deterministic/version.rb +1 -1
- data/spec/lib/deterministic/either/chain_spec.rb +22 -2
- data/spec/lib/deterministic/either/failure_spec.rb +6 -0
- data/spec/lib/deterministic/either/success_spec.rb +6 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e4df729f9e00305be8b4bfa828c627dd581ad07
|
4
|
+
data.tar.gz: 0e1c862ea8004b24bbe65d1d86aa93946013fdad
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bef331eed029917b69da1e1059b75f2d43a63ce00bac59f16e589e78de61349e57624f57bc84dac8b7011045043fae28f2fa74831b7d13fea07e2f9f90b805b9
|
7
|
+
data.tar.gz: adb613df744063e6ac84bee504c2c050d70ace2add63a8b93978efbc128f84e78fee9e205c983f90de7025d7511591e802f3286a3573c314644673b640ad09b7
|
data/README.md
CHANGED
@@ -107,6 +107,16 @@ end
|
|
107
107
|
Success(0) >> method(:works) >> method(:breaks) >> method(:never_executed) # Failure(2)
|
108
108
|
```
|
109
109
|
|
110
|
+
`#chain` aka `#>>` will not catch any exceptions raised. If you want automatic exception handling, the `#try` aka `#>=` will catch an error and wrap it with a failure
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
def error(ctx)
|
114
|
+
raise "error #{1}"
|
115
|
+
end
|
116
|
+
|
117
|
+
Success(1) >= method(:error) # Failure(RuntimeError(error 1))
|
118
|
+
```
|
119
|
+
|
110
120
|
### Either.attempt_all
|
111
121
|
The basic idea is to execute a chain of units of work and make sure all return either `Success` or `Failure`.
|
112
122
|
This remains for compatibility reasons, personally I would use the `>>` chaining.
|
@@ -3,13 +3,16 @@ require 'spec_helper'
|
|
3
3
|
include Deterministic
|
4
4
|
|
5
5
|
describe Deterministic::Either do
|
6
|
-
context ">>" do
|
6
|
+
context ">> (chain)" do
|
7
7
|
it "Failure stops execution" do
|
8
8
|
class ChainUnderTest
|
9
9
|
alias :m :method
|
10
10
|
|
11
11
|
def call
|
12
|
-
init >>
|
12
|
+
init >>
|
13
|
+
m(:validate) >>
|
14
|
+
m(:send) >>
|
15
|
+
m(:parse)
|
13
16
|
end
|
14
17
|
|
15
18
|
def init
|
@@ -56,5 +59,22 @@ describe Deterministic::Either do
|
|
56
59
|
Success(1) >> ->(i) { Success(i + 1) }
|
57
60
|
).to eq Success(2)
|
58
61
|
end
|
62
|
+
|
63
|
+
it "does not catch exceptions" do
|
64
|
+
expect {
|
65
|
+
Success(1) >> ->(i) { raise "error" }
|
66
|
+
}.to raise_error(RuntimeError)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context ">= (try)" do
|
71
|
+
it "try (>=) catches errors and wraps them as Failure" do
|
72
|
+
def error(ctx)
|
73
|
+
raise "error #{ctx}"
|
74
|
+
end
|
75
|
+
|
76
|
+
actual = Success(1) >= method(:error)
|
77
|
+
expect(actual.inspect).to eq "Failure(error 1)"
|
78
|
+
end
|
59
79
|
end
|
60
80
|
end
|
@@ -22,6 +22,12 @@ describe Deterministic::Failure do
|
|
22
22
|
specify { expect(subject << Failure(2)).to eq(Failure(1)) }
|
23
23
|
specify { expect(subject.map { |v| v + 1} ).to eq Failure(2) }
|
24
24
|
|
25
|
+
specify { expect(subject.or(Success(2))).to eq Success(2)}
|
26
|
+
specify { expect(subject.or_else { Success(2) }).to eq Success(2)}
|
27
|
+
|
28
|
+
specify { expect(subject.and(Success(2))).to eq Failure(1)}
|
29
|
+
specify { expect(subject.and_then { Success(2) }).to eq Failure(1)}
|
30
|
+
|
25
31
|
it_behaves_like 'Either' do
|
26
32
|
let(:either) { described_class }
|
27
33
|
end
|
@@ -22,6 +22,12 @@ describe Deterministic::Success do
|
|
22
22
|
specify { expect(subject << Failure(2)).to eq(Failure(2)) }
|
23
23
|
specify { expect(subject.map { |v| v + 1} ).to eq Success(2) }
|
24
24
|
|
25
|
+
specify { expect(subject.or(Success(2))).to eq Success(1)}
|
26
|
+
specify { expect(subject.or_else { Success(2) }).to eq Success(1)}
|
27
|
+
|
28
|
+
specify { expect(subject.and(Success(2))).to eq Success(2)}
|
29
|
+
specify { expect(subject.and_then { Success(2) }).to eq Success(2)}
|
30
|
+
|
25
31
|
it_behaves_like 'Either' do
|
26
32
|
let(:either) { described_class }
|
27
33
|
end
|