deterministic 0.6.0 → 0.7.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/README.md +27 -25
- data/lib/deterministic/either/attempt_all.rb +6 -1
- data/lib/deterministic/none.rb +1 -1
- data/lib/deterministic/version.rb +1 -1
- data/spec/lib/deterministic/attempt_all_spec.rb +10 -0
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a8e5d5bcf60bb0a88e48107853dbc0b38aa0495b
|
4
|
+
data.tar.gz: b319c46c0356abc592203b5d364c5f033bc90045
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90736736f45c37196aa250de61a38a43d5e1c4d58c9fa517d0f7a96cb31a7e3596d647d01c4ac3946829204bed5164d360bb0257620417b7fb71543c20c0d77f
|
7
|
+
data.tar.gz: 04e675507417f1af47fcdeb4ababb511b665b320ee570780ff204e88482f6fe5bab16b087212a9222d783a1f12add100fac74c22d21f833f8617c83806d30877
|
data/README.md
CHANGED
@@ -2,9 +2,10 @@
|
|
2
2
|
|
3
3
|
[](http://badge.fury.io/rb/deterministic)
|
4
4
|
|
5
|
+
Deterministic is to help your code to be more confident, it's specialty is flow control of actions that either succeed or fail.
|
6
|
+
|
5
7
|
This is a spiritual successor of the [Monadic gem](http://github.com/pzol/monadic). The goal of the rewrite is to get away from a bit to forceful aproach I took in Monadic, especially when it comes to coercing monads, but also a more practical but at the same time more strict adherence to monad laws.
|
6
8
|
|
7
|
-
This gem is still __WORK IN PROGRESS__.
|
8
9
|
|
9
10
|
## Usage
|
10
11
|
|
@@ -24,6 +25,30 @@ Failure(1).map { |v| v + 1} # => Failure(2)
|
|
24
25
|
Failure({a:1}).to_json # => '{"Failure": {"a":1}}'
|
25
26
|
```
|
26
27
|
|
28
|
+
Chaining successful actions
|
29
|
+
|
30
|
+
```ruby
|
31
|
+
Success(1).and Success(2) # => Success(2)
|
32
|
+
Success(1).and_then { Success(2) } # => Success(2)
|
33
|
+
|
34
|
+
Success(1).or Success(2) # => Success(1)
|
35
|
+
Success(1).or_else { Success(2) } # => Success(1)
|
36
|
+
```
|
37
|
+
|
38
|
+
Chaining failed actions
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
Failure(1).and Success(2) # => Failure(1)
|
42
|
+
Failure(1).and_then { Success(2) } # => Failure(1)
|
43
|
+
|
44
|
+
Failure(1).or Success(1) # => Success(1)
|
45
|
+
Failure(1).or_else { |v| Success(v)} # => Success(1)
|
46
|
+
```
|
47
|
+
|
48
|
+
The value or block result must always be a `Success` or `Failure`
|
49
|
+
|
50
|
+
|
51
|
+
|
27
52
|
### Either.attempt_all
|
28
53
|
The basic idea is to execute a chain of units of work and make sure all return either `Success` or `Failure`.
|
29
54
|
|
@@ -150,30 +175,6 @@ Success(1).match do
|
|
150
175
|
end # => "catch-all"
|
151
176
|
```
|
152
177
|
|
153
|
-
## Flow control
|
154
|
-
|
155
|
-
Chaininig successfull actions
|
156
|
-
|
157
|
-
```ruby
|
158
|
-
Success(1).and Success(2) # => Success(2)
|
159
|
-
Success(1).and_then { Success(2) } # => Success(2)
|
160
|
-
|
161
|
-
Success(1).or Success(2) # => Success(1)
|
162
|
-
Success(1).or_else { Success(2) } # => Success(1)
|
163
|
-
```
|
164
|
-
|
165
|
-
Chaininig failed actions
|
166
|
-
|
167
|
-
```ruby
|
168
|
-
Failure(1).and Success(2) # => Failure(1)
|
169
|
-
Failure(1).and_then { Success(2) } # => Failure(1)
|
170
|
-
|
171
|
-
Failure(1).or Success(1) # => Success(1)
|
172
|
-
Failure(1).or_else { |v| Success(v)} # => Success(1)
|
173
|
-
```
|
174
|
-
|
175
|
-
The value or block result must always be a `Success` or `Failure`
|
176
|
-
|
177
178
|
## core_ext
|
178
179
|
You can use a core extension, to include Either in your own class or in Object, i.e. in all classes.
|
179
180
|
|
@@ -266,6 +267,7 @@ null.foo # => NoMethodError
|
|
266
267
|
* [either by rsslldnphy](https://github.com/rsslldnphy/either)
|
267
268
|
* [Functors, Applicatives, And Monads In Pictures](http://adit.io/posts/2013-04-17-functors,_applicatives,_and_monads_in_pictures.html)
|
268
269
|
* [Naught by avdi](https://github.com/avdi/naught/)
|
270
|
+
* [Rust's Result](http://static.rust-lang.org/doc/master/std/result/enum.Result.html)
|
269
271
|
|
270
272
|
## Installation
|
271
273
|
|
@@ -24,7 +24,12 @@ class Deterministic::Either
|
|
24
24
|
try_p = ->(acc) {
|
25
25
|
begin
|
26
26
|
value = @context.instance_exec(acc.value, &block)
|
27
|
-
|
27
|
+
case value
|
28
|
+
when Success, Failure
|
29
|
+
value
|
30
|
+
else
|
31
|
+
Deterministic::Success(value)
|
32
|
+
end
|
28
33
|
rescue => ex
|
29
34
|
Deterministic::Failure(ex)
|
30
35
|
end
|
data/lib/deterministic/none.rb
CHANGED
@@ -72,6 +72,16 @@ describe Deterministic::Either::AttemptAll do
|
|
72
72
|
).to eq Success(2)
|
73
73
|
end
|
74
74
|
|
75
|
+
|
76
|
+
it "works with boxed values" do
|
77
|
+
expect(
|
78
|
+
Either.attempt_all do
|
79
|
+
try { Failure(1) }
|
80
|
+
try { 2 }
|
81
|
+
end
|
82
|
+
).to eq Failure(1)
|
83
|
+
end
|
84
|
+
|
75
85
|
it "works with an OpenStruct" do
|
76
86
|
context = OpenStruct.new
|
77
87
|
attempt = Either.attempt_all(context) do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: deterministic
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Piotr Zolnierek
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-08-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -171,7 +171,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
171
171
|
version: '0'
|
172
172
|
requirements: []
|
173
173
|
rubyforge_project:
|
174
|
-
rubygems_version: 2.2.
|
174
|
+
rubygems_version: 2.2.2
|
175
175
|
signing_key:
|
176
176
|
specification_version: 4
|
177
177
|
summary: see above
|