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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 02fd1f0c22527cf4af1af78fdb8e868d4947fb3b
4
- data.tar.gz: 0efb14c1c44f3d487e551765a1fb671b8e9b55c7
3
+ metadata.gz: 7e4df729f9e00305be8b4bfa828c627dd581ad07
4
+ data.tar.gz: 0e1c862ea8004b24bbe65d1d86aa93946013fdad
5
5
  SHA512:
6
- metadata.gz: cb9ab93ae058be25b8f724d4c246138ce6f576cf59f6bcf8e6927a97fbbd67f42fff1a527f7bcf791d858fd03d1108a8f756457a3bb0b345c82b5aa83c5ef7d2
7
- data.tar.gz: a40bc7baf45c6785b0204faca45f9e5c3a4eb4f33e0baae1c647da5591bc464051bb58f8b109c09518c6aad80ae1f112d5a7659ea0df1d721db3d3ae38d55e86
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.
@@ -7,7 +7,16 @@ module Deterministic
7
7
  end
8
8
 
9
9
  alias :>> :chain
10
+
11
+ def try(proc=nil, &block)
12
+ return self if failure?
13
+ bind { (proc || block).call(@value) }
14
+ rescue => err
15
+ Failure(err)
10
16
  end
17
+
18
+ alias :>= :try
19
+ end
11
20
  end
12
21
  end
13
22
 
@@ -1,3 +1,3 @@
1
1
  module Deterministic
2
- VERSION = "0.8.0"
2
+ VERSION = "0.8.1"
3
3
  end
@@ -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 >> m(:validate) >> m(:send) >> m(:parse)
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deterministic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Zolnierek