fear 0.4.1 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 44b1ffdd8f0d5e74fe8a0a321976fc63cea3744d
4
- data.tar.gz: 8a3a25e30333937b404297247d02e2875d3c2769
3
+ metadata.gz: fe1da9cca954b338585a16edadec505294704e48
4
+ data.tar.gz: d2b9f1d9d4a186b5d9aa622dd9509a1aacdf545f
5
5
  SHA512:
6
- metadata.gz: 6fb277d637d1438a0471d586f81dbbbd2348a4bfa05873e65c74f9836c0040d7625afb6f0e93acd000ae893356f9088b4a03e1ec19881074f9e1527b634a886e
7
- data.tar.gz: b763ec95bcb614cacb81f6d4d6ef27bf84d90cb3ab7dd3d299263b1a009ff9201eb3462a0e7b5fbb52fe3c84ae372295ec9eea7afa0443c49ec33fa6529b0de2
6
+ metadata.gz: 5a65ed502c031f9f028d8bdc2b62b0c90c5d40aaf0efb6eb56755251fd967b11c3d9955d0c5473bf4a8b9c252dfd8f0141adc4c0bc09fa0cbb89ba65d99a4200
7
+ data.tar.gz: 5ee2ea5cc16bd2ef862a9eb2a25303c09bcaa125ba6fcbac2a8ac50859551ee1d13dd87bd57923865c3951c97c824f18b9c3111d9b923cc568eb848016e98389
data/.rubocop.yml CHANGED
@@ -6,3 +6,6 @@ Style/MethodName:
6
6
 
7
7
  Style/Documentation:
8
8
  Enabled: false
9
+
10
+ Style/CaseEquality:
11
+ Enabled: false
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(-1)
147
- # Left(12).select_or_else(-> { -1 }, &:even?) #=> Left(-1)
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(default)
21
- Left.new(Utils.return_or_call_proc(default))
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
@@ -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
@@ -249,7 +249,6 @@ module Fear
249
249
  # @return [Failure]
250
250
  #
251
251
  def Failure(exception)
252
- fail TypeError, "not an error: #{exception}" unless exception.is_a?(StandardError)
253
252
  Failure.new(exception)
254
253
  end
255
254
 
data/lib/fear/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fear
2
- VERSION = '0.4.1'.freeze
2
+ VERSION = '0.4.2'.freeze
3
3
  end
@@ -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
@@ -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 Left of evaluated default' do
29
- is_expected.to eq(described_class.new(-1))
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 'return Left of default' do
37
- is_expected.to eq(described_class.new(-1))
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.1
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-16 00:00:00.000000000 Z
11
+ date: 2016-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-equalizer