fear 0.4.1 → 0.4.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/.rubocop.yml +3 -0
- data/README.md +26 -0
- data/lib/fear/either.rb +2 -2
- data/lib/fear/failure.rb +11 -0
- data/lib/fear/left.rb +13 -3
- data/lib/fear/right_biased.rb +11 -0
- data/lib/fear/try.rb +0 -1
- data/lib/fear/version.rb +1 -1
- data/spec/fear/failure_spec.rb +24 -0
- data/spec/fear/left_spec.rb +28 -4
- data/spec/fear/right_biased/left.rb +9 -0
- data/spec/fear/right_biased/right.rb +29 -0
- 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: fe1da9cca954b338585a16edadec505294704e48
|
4
|
+
data.tar.gz: d2b9f1d9d4a186b5d9aa622dd9509a1aacdf545f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5a65ed502c031f9f028d8bdc2b62b0c90c5d40aaf0efb6eb56755251fd967b11c3d9955d0c5473bf4a8b9c252dfd8f0141adc4c0bc09fa0cbb89ba65d99a4200
|
7
|
+
data.tar.gz: 5ee2ea5cc16bd2ef862a9eb2a25303c09bcaa125ba6fcbac2a8ac50859551ee1d13dd87bd57923865c3951c97c824f18b9c3111d9b923cc568eb848016e98389
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -162,6 +162,23 @@ would be translated to:
|
|
162
162
|
end
|
163
163
|
```
|
164
164
|
|
165
|
+
### Pattern Matching
|
166
|
+
|
167
|
+
`Option`, `Either`, and `Try` contains enhanced version of `#===` method. It performs matching not
|
168
|
+
only on container itself, but on enclosed value as well. I'm writing all the options in a one
|
169
|
+
case statement in sake of simplicity.
|
170
|
+
|
171
|
+
```ruby
|
172
|
+
case Some(42)
|
173
|
+
when Some(42) #=> matches
|
174
|
+
when Some(41) #=> does not match
|
175
|
+
when Some(Fixnum) #=> matches
|
176
|
+
when Some(String) #=> does not match
|
177
|
+
when Some((40..43)) #=> matches
|
178
|
+
when Some(-> (x) { x > 40 }) #=> matches
|
179
|
+
end
|
180
|
+
```
|
181
|
+
|
165
182
|
## Testing
|
166
183
|
|
167
184
|
To simplify testing, you may use [fear-rspec](https://github.com/bolshakov/fear-rspec) gem. It
|
@@ -174,3 +191,12 @@ provides a bunch of rspec matchers.
|
|
174
191
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
175
192
|
4. Push to the branch (`git push origin my-new-feature`)
|
176
193
|
5. Create a new Pull Request
|
194
|
+
|
195
|
+
## Alternatives
|
196
|
+
|
197
|
+
* [dry-monads](https://github.com/dry-rb/dry-monads)
|
198
|
+
* [deterministic](https://github.com/pzol/deterministic)
|
199
|
+
* [ruby-possibly](https://github.com/rap1ds/ruby-possibly)
|
200
|
+
* [kleisli](https://github.com/txus/kleisli)
|
201
|
+
* [rumonade](https://github.com/ms-ati/rumonade)
|
202
|
+
|
data/lib/fear/either.rb
CHANGED
@@ -143,8 +143,8 @@ module Fear
|
|
143
143
|
# @example
|
144
144
|
# Right(12).select_or_else(-1, &:even?) #=> Right(12)
|
145
145
|
# Right(7).select_or_else(-1, &:even?) #=> Left(-1)
|
146
|
-
# Left(12).select_or_else(-1, &:even?) #=> Left(
|
147
|
-
# Left(12).select_or_else(-> { -1 }, &:even?) #=> Left(
|
146
|
+
# Left(12).select_or_else(-1, &:even?) #=> Left(12)
|
147
|
+
# Left(12).select_or_else(-> { -1 }, &:even?) #=> Left(12)
|
148
148
|
#
|
149
149
|
# @!method select(&predicate)
|
150
150
|
# Returns +Left+ of value if the given predicate
|
data/lib/fear/failure.rb
CHANGED
@@ -68,5 +68,16 @@ module Fear
|
|
68
68
|
def to_either
|
69
69
|
Left.new(value)
|
70
70
|
end
|
71
|
+
|
72
|
+
# Used in case statement
|
73
|
+
# @param other [any]
|
74
|
+
# @return [Boolean]
|
75
|
+
def ===(other)
|
76
|
+
if other.is_a?(Failure)
|
77
|
+
value === other.value
|
78
|
+
else
|
79
|
+
super
|
80
|
+
end
|
81
|
+
end
|
71
82
|
end
|
72
83
|
end
|
data/lib/fear/left.rb
CHANGED
@@ -15,10 +15,9 @@ module Fear
|
|
15
15
|
end
|
16
16
|
alias failure? left?
|
17
17
|
|
18
|
-
# @param default [Proc, any]
|
19
18
|
# @return [Either]
|
20
|
-
def select_or_else(
|
21
|
-
|
19
|
+
def select_or_else(*)
|
20
|
+
self
|
22
21
|
end
|
23
22
|
|
24
23
|
# @return [Left]
|
@@ -54,5 +53,16 @@ module Fear
|
|
54
53
|
Utils.assert_type!(v, Either)
|
55
54
|
end
|
56
55
|
end
|
56
|
+
|
57
|
+
# Used in case statement
|
58
|
+
# @param other [any]
|
59
|
+
# @return [Boolean]
|
60
|
+
def ===(other)
|
61
|
+
if other.is_a?(Left)
|
62
|
+
value === other.value
|
63
|
+
else
|
64
|
+
super
|
65
|
+
end
|
66
|
+
end
|
57
67
|
end
|
58
68
|
end
|
data/lib/fear/right_biased.rb
CHANGED
@@ -85,6 +85,17 @@ module Fear
|
|
85
85
|
def any?
|
86
86
|
yield(value)
|
87
87
|
end
|
88
|
+
|
89
|
+
# Used in case statement
|
90
|
+
# @param other [any]
|
91
|
+
# @return [Boolean]
|
92
|
+
def ===(other)
|
93
|
+
if other.is_a?(right_class)
|
94
|
+
value === other.value
|
95
|
+
else
|
96
|
+
super
|
97
|
+
end
|
98
|
+
end
|
88
99
|
end
|
89
100
|
|
90
101
|
module Left
|
data/lib/fear/try.rb
CHANGED
data/lib/fear/version.rb
CHANGED
data/spec/fear/failure_spec.rb
CHANGED
@@ -86,4 +86,28 @@ RSpec.describe Fear::Failure do
|
|
86
86
|
subject { failure.to_either }
|
87
87
|
it { is_expected.to eq(Left(exception)) }
|
88
88
|
end
|
89
|
+
|
90
|
+
describe '#===' do
|
91
|
+
subject { match === failure }
|
92
|
+
|
93
|
+
context 'matches erectly' do
|
94
|
+
let(:match) { Failure(exception) }
|
95
|
+
it { is_expected.to eq(true) }
|
96
|
+
end
|
97
|
+
|
98
|
+
context 'value does not match' do
|
99
|
+
let(:match) { Failure(ArgumentError.new) }
|
100
|
+
it { is_expected.to eq(false) }
|
101
|
+
end
|
102
|
+
|
103
|
+
context 'matches by class' do
|
104
|
+
let(:match) { Failure(RuntimeError) }
|
105
|
+
it { is_expected.to eq(true) }
|
106
|
+
end
|
107
|
+
|
108
|
+
context 'does not matches by class' do
|
109
|
+
let(:match) { Failure(ArgumentError) }
|
110
|
+
it { is_expected.to eq(false) }
|
111
|
+
end
|
112
|
+
end
|
89
113
|
end
|
data/spec/fear/left_spec.rb
CHANGED
@@ -25,16 +25,16 @@ RSpec.describe Fear::Left do
|
|
25
25
|
context 'proc default' do
|
26
26
|
let(:default) { -> { -1 } }
|
27
27
|
|
28
|
-
it 'returns
|
29
|
-
is_expected.to eq(
|
28
|
+
it 'returns itself' do
|
29
|
+
is_expected.to eq(left)
|
30
30
|
end
|
31
31
|
end
|
32
32
|
|
33
33
|
context 'default' do
|
34
34
|
let(:default) { -1 }
|
35
35
|
|
36
|
-
it '
|
37
|
-
is_expected.to eq(
|
36
|
+
it 'returns itself' do
|
37
|
+
is_expected.to eq(left)
|
38
38
|
end
|
39
39
|
end
|
40
40
|
end
|
@@ -108,4 +108,28 @@ RSpec.describe Fear::Left do
|
|
108
108
|
end
|
109
109
|
end
|
110
110
|
end
|
111
|
+
|
112
|
+
describe '#===' do
|
113
|
+
subject { match === left }
|
114
|
+
|
115
|
+
context 'matches erectly' do
|
116
|
+
let(:match) { Left('value') }
|
117
|
+
it { is_expected.to eq(true) }
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'value does not match' do
|
121
|
+
let(:match) { Left('error') }
|
122
|
+
it { is_expected.to eq(false) }
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'matches by class' do
|
126
|
+
let(:match) { Left(String) }
|
127
|
+
it { is_expected.to eq(true) }
|
128
|
+
end
|
129
|
+
|
130
|
+
context 'does not matches by class' do
|
131
|
+
let(:match) { Left(Integer) }
|
132
|
+
it { is_expected.to eq(false) }
|
133
|
+
end
|
134
|
+
end
|
111
135
|
end
|
@@ -64,4 +64,13 @@ RSpec.shared_examples Fear::RightBiased::Left do
|
|
64
64
|
subject { left.any? { |v| v == 'value' } }
|
65
65
|
it { is_expected.to eq(false) }
|
66
66
|
end
|
67
|
+
|
68
|
+
describe '#===' do
|
69
|
+
subject { match === left }
|
70
|
+
|
71
|
+
context 'the same object' do
|
72
|
+
let(:match) { left }
|
73
|
+
it { is_expected.to eq(true) }
|
74
|
+
end
|
75
|
+
end
|
67
76
|
end
|
@@ -90,4 +90,33 @@ RSpec.shared_examples Fear::RightBiased::Right do
|
|
90
90
|
it { is_expected.to eq(false) }
|
91
91
|
end
|
92
92
|
end
|
93
|
+
|
94
|
+
describe '#===' do
|
95
|
+
subject { match === right }
|
96
|
+
|
97
|
+
context 'matches erectly' do
|
98
|
+
let(:match) { described_class.new('value') }
|
99
|
+
it { is_expected.to eq(true) }
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'the same object' do
|
103
|
+
let(:match) { right }
|
104
|
+
it { is_expected.to eq(true) }
|
105
|
+
end
|
106
|
+
|
107
|
+
context 'value does not match' do
|
108
|
+
let(:match) { described_class.new('error') }
|
109
|
+
it { is_expected.to eq(false) }
|
110
|
+
end
|
111
|
+
|
112
|
+
context 'matches by class' do
|
113
|
+
let(:match) { described_class.new(String) }
|
114
|
+
it { is_expected.to eq(true) }
|
115
|
+
end
|
116
|
+
|
117
|
+
context 'does not matches by class' do
|
118
|
+
let(:match) { described_class.new(Integer) }
|
119
|
+
it { is_expected.to eq(false) }
|
120
|
+
end
|
121
|
+
end
|
93
122
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fear
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Tema Bolshakov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-12-
|
11
|
+
date: 2016-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-equalizer
|