fear 0.2.0 → 0.3.0

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: b856c96f41e1cab50482a618c021f32eccd16d97
4
- data.tar.gz: 7e443f61c506e29e1578f64e7f2237664c1465ec
3
+ metadata.gz: 3ab163e8a3ccc420c6c1436e42c269e36e333b36
4
+ data.tar.gz: ac6d7a735f7c6ac3f20194438252b59fbdecf8c4
5
5
  SHA512:
6
- metadata.gz: fef37c2dd16a69752985bd04d734ab27a69513e9db7a114c93dfb66116473d549e3a3106a0221e1f1e61ad0fa1192291cfb9e76eb7fbfc5af7297540d167610c
7
- data.tar.gz: ff910daab541a9279e7838a971db37d9a37f6bdb220e6b17e985a9e3daaebd61cbba31167efc3e1d7b9201958310270b9788597ecc284b61f0f30bf20586e8f6
6
+ metadata.gz: 8a8f2500d2c88ba91388420a4d8e2f29ccc8d912b77c5c75484e1ff472dd1ca4ec64dbd9dc537e116845972dab0659219dc2aea9f549494d5266129771c3bbc0
7
+ data.tar.gz: a3c52d900667bd52580d2983cddafcb2e796524b19b418e4722fca99b520ce13232fc4ce18c433e01d653d1b5f3346ba5a27c7417114d807fe37b7ea16ac1db9
data/README.md CHANGED
@@ -162,6 +162,11 @@ would be translated to:
162
162
  end
163
163
  ```
164
164
 
165
+ ## Testing
166
+
167
+ To simplify testing, you may use [fear-rspec](https://github.com/bolshakov/fear-rspec) gem. It
168
+ provides a bunch of rspec matchers.
169
+
165
170
  ## Contributing
166
171
 
167
172
  1. Fork it ( https://github.com/bolshakov/fear/fork )
data/fear.gemspec CHANGED
@@ -26,4 +26,5 @@ Gem::Specification.new do |spec|
26
26
  spec.add_development_dependency 'rspec', '~> 3.1'
27
27
  spec.add_development_dependency 'spbtv_code_style'
28
28
  spec.add_development_dependency 'rubocop-rspec'
29
+ spec.add_development_dependency 'simplecov'
29
30
  end
data/lib/fear/either.rb CHANGED
@@ -158,6 +158,18 @@ module Fear
158
158
  # Left(12).select(&:even?) #=> Left(12)
159
159
  # Left(7).select(&:even?) #=> Left(7)
160
160
  #
161
+ # @!method reject(&predicate)
162
+ # Returns +Left+ of value if the given predicate holds for the
163
+ # right value, otherwise, returns +Right+.
164
+ # @yieldparam value [Object]
165
+ # @yieldreturn [Boolean]
166
+ # @return [Either]
167
+ # @example
168
+ # Right(12).reject(&:even?) #=> Left(12)
169
+ # Right(7).reject(&:even?) #=> Right(7)
170
+ # Left(12).reject(&:even?) #=> Left(12)
171
+ # Left(7).reject(&:even?) #=> Left(7)
172
+ #
161
173
  # @!method swap
162
174
  # If this is a +Left+, then return the left value in +Right+ or vice versa.
163
175
  # @return [Either]
data/lib/fear/left.rb CHANGED
@@ -26,6 +26,11 @@ module Fear
26
26
  self
27
27
  end
28
28
 
29
+ # @return [Left]
30
+ def reject
31
+ self
32
+ end
33
+
29
34
  # @return [Right] value in `Right`
30
35
  def swap
31
36
  Right.new(value)
data/lib/fear/right.rb CHANGED
@@ -34,6 +34,15 @@ module Fear
34
34
  end
35
35
  end
36
36
 
37
+ # @return [Either]
38
+ def reject
39
+ if yield(value)
40
+ Left.new(value)
41
+ else
42
+ self
43
+ end
44
+ end
45
+
37
46
  # @return [Left] value in `Left`
38
47
  def swap
39
48
  Left.new(value)
data/lib/fear/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Fear
2
- VERSION = '0.2.0'.freeze
2
+ VERSION = '0.3.0'.freeze
3
3
  end
@@ -1,6 +1,6 @@
1
1
  RSpec.describe Fear::Failure do
2
2
  let(:exception) { RuntimeError.new('error') }
3
- let(:failure) { described_class.new(exception) }
3
+ let(:failure) { Failure(exception) }
4
4
 
5
5
  it_behaves_like Fear::RightBiased::Left do
6
6
  let(:left) { failure }
@@ -11,6 +11,11 @@ RSpec.describe Fear::Failure do
11
11
  it { is_expected.not_to be_success }
12
12
  end
13
13
 
14
+ describe '#failure?' do
15
+ subject { failure }
16
+ it { is_expected.to be_failure }
17
+ end
18
+
14
19
  describe '#get' do
15
20
  subject { proc { failure.get } }
16
21
  it { is_expected.to raise_error(RuntimeError, 'error') }
@@ -43,7 +48,7 @@ RSpec.describe Fear::Failure do
43
48
  context 'block does not fail' do
44
49
  subject do
45
50
  failure.recover_with do |error|
46
- Fear::Success.new(error.message)
51
+ Success(error.message)
47
52
  end
48
53
  end
49
54
 
@@ -65,7 +70,7 @@ RSpec.describe Fear::Failure do
65
70
  subject { failure.recover(&:message) }
66
71
 
67
72
  it 'returns Success of evaluation of the block against the error' do
68
- is_expected.to eq(Fear::Success.new('error'))
73
+ is_expected.to eq(Success('error'))
69
74
  end
70
75
  end
71
76
 
@@ -79,6 +84,6 @@ RSpec.describe Fear::Failure do
79
84
 
80
85
  describe '#to_either' do
81
86
  subject { failure.to_either }
82
- it { is_expected.to eq(Fear::Left.new(exception)) }
87
+ it { is_expected.to eq(Left(exception)) }
83
88
  end
84
89
  end
@@ -1,21 +1,19 @@
1
1
  RSpec.describe Fear::For do
2
- include Fear::For::Mixin
3
-
4
2
  context 'unary' do
5
3
  context 'Some' do
6
4
  subject do
7
- For(a: Fear::Some.new(2)) { a * 2 }
5
+ For(a: Some(2)) { a * 2 }
8
6
  end
9
7
 
10
- it { is_expected.to eq(Fear::Some.new(4)) }
8
+ it { is_expected.to eq(Some(4)) }
11
9
  end
12
10
 
13
11
  context 'None' do
14
12
  subject do
15
- For(a: Fear::None.new) { a * 2 }
13
+ For(a: None()) { a * 2 }
16
14
  end
17
15
 
18
- it { is_expected.to eq(Fear::None.new) }
16
+ it { is_expected.to eq(None()) }
19
17
  end
20
18
  end
21
19
 
@@ -36,35 +34,35 @@ RSpec.describe Fear::For do
36
34
  end
37
35
 
38
36
  context 'all Same' do
39
- let(:first) { Fear::Some.new(2) }
40
- let(:second) { Fear::Some.new(3) }
41
- let(:third) { Fear::Some.new(4) }
37
+ let(:first) { Some(2) }
38
+ let(:second) { Some(3) }
39
+ let(:third) { Some(4) }
42
40
 
43
- it { is_expected.to eq(Fear::Some.new(24)) }
41
+ it { is_expected.to eq(Some(24)) }
44
42
  end
45
43
 
46
44
  context 'first None' do
47
- let(:first) { Fear::None.new }
48
- let(:second) { Fear::Some.new(3) }
49
- let(:third) { Fear::Some.new(4) }
45
+ let(:first) { None() }
46
+ let(:second) { Some(3) }
47
+ let(:third) { Some(4) }
50
48
 
51
- it { is_expected.to eq(Fear::None.new) }
49
+ it { is_expected.to eq(None()) }
52
50
  end
53
51
 
54
52
  context 'second None' do
55
- let(:first) { Fear::Some.new(2) }
56
- let(:second) { Fear::None.new }
57
- let(:third) { Fear::Some.new(4) }
53
+ let(:first) { Some(2) }
54
+ let(:second) { None() }
55
+ let(:third) { Some(4) }
58
56
 
59
- it { is_expected.to eq(Fear::None.new) }
57
+ it { is_expected.to eq(None()) }
60
58
  end
61
59
 
62
60
  context 'last None' do
63
- let(:first) { Fear::Some.new(2) }
64
- let(:second) { Fear::Some.new(3) }
65
- let(:third) { Fear::None.new }
61
+ let(:first) { Some(2) }
62
+ let(:second) { Some(3) }
63
+ let(:third) { None() }
66
64
 
67
- it { is_expected.to eq(Fear::None.new) }
65
+ it { is_expected.to eq(None()) }
68
66
  end
69
67
  end
70
68
  end
@@ -2,10 +2,10 @@ RSpec.describe Fear::Left do
2
2
  include Fear::Either::Mixin
3
3
 
4
4
  it_behaves_like Fear::RightBiased::Left do
5
- let(:left) { described_class.new('value') }
5
+ let(:left) { Left('value') }
6
6
  end
7
7
 
8
- let(:left) { described_class.new('value') }
8
+ let(:left) { Left('value') }
9
9
 
10
10
  describe '#right?' do
11
11
  subject { left }
@@ -49,6 +49,16 @@ RSpec.describe Fear::Left do
49
49
  end
50
50
  end
51
51
 
52
+ describe '#reject' do
53
+ subject do
54
+ left.reject { |v| v == 'value' }
55
+ end
56
+
57
+ it 'return self' do
58
+ is_expected.to eq(left)
59
+ end
60
+ end
61
+
52
62
  describe '#swap' do
53
63
  subject { left.swap }
54
64
  it { is_expected.to eq(Right('value')) }
@@ -2,7 +2,7 @@ RSpec.describe Fear::None do
2
2
  include Fear::Option::Mixin
3
3
 
4
4
  it_behaves_like Fear::RightBiased::Left do
5
- let(:left) { described_class.new }
5
+ let(:left) { None() }
6
6
  end
7
7
 
8
8
  subject(:none) { None() }
@@ -1,9 +1,9 @@
1
1
  RSpec.describe Fear::Right do
2
2
  it_behaves_like Fear::RightBiased::Right do
3
- let(:right) { described_class.new('value') }
3
+ let(:right) { Right('value') }
4
4
  end
5
5
 
6
- let(:right) { described_class.new('value') }
6
+ let(:right) { Right('value') }
7
7
 
8
8
  describe '#right?' do
9
9
  subject { right }
@@ -27,13 +27,13 @@ RSpec.describe Fear::Right do
27
27
  context 'predicate evaluates to false and default is a proc' do
28
28
  let(:predicate) { ->(v) { v != 'value' } }
29
29
  let(:default) { -> { -1 } }
30
- it { is_expected.to eq(Fear::Left.new(-1)) }
30
+ it { is_expected.to eq(Left(-1)) }
31
31
  end
32
32
 
33
33
  context 'predicate evaluates to false and default is not a proc' do
34
34
  let(:predicate) { ->(v) { v != 'value' } }
35
35
  let(:default) { -1 }
36
- it { is_expected.to eq(Fear::Left.new(-1)) }
36
+ it { is_expected.to eq(Left(-1)) }
37
37
  end
38
38
  end
39
39
 
@@ -47,13 +47,27 @@ RSpec.describe Fear::Right do
47
47
 
48
48
  context 'predicate evaluates to false' do
49
49
  let(:predicate) { ->(v) { v != 'value' } }
50
- it { is_expected.to eq(Fear::Left.new('value')) }
50
+ it { is_expected.to eq(Left('value')) }
51
+ end
52
+ end
53
+
54
+ describe '#reject' do
55
+ subject { right.reject(&predicate) }
56
+
57
+ context 'predicate evaluates to true' do
58
+ let(:predicate) { ->(v) { v == 'value' } }
59
+ it { is_expected.to eq(Left('value')) }
60
+ end
61
+
62
+ context 'predicate evaluates to false' do
63
+ let(:predicate) { ->(v) { v != 'value' } }
64
+ it { is_expected.to eq(right) }
51
65
  end
52
66
  end
53
67
 
54
68
  describe '#swap' do
55
69
  subject { right.swap }
56
- it { is_expected.to eq(Fear::Left.new('value')) }
70
+ it { is_expected.to eq(Left('value')) }
57
71
  end
58
72
 
59
73
  describe '#reduce' do
@@ -70,7 +84,7 @@ RSpec.describe Fear::Right do
70
84
  describe '#join_right' do
71
85
  context 'value is Either' do
72
86
  subject { described_class.new(value).join_right }
73
- let(:value) { Fear::Left.new('error') }
87
+ let(:value) { Left('error') }
74
88
 
75
89
  it 'returns value' do
76
90
  is_expected.to eq(value)
@@ -89,7 +103,7 @@ RSpec.describe Fear::Right do
89
103
  describe '#join_left' do
90
104
  context 'value is Either' do
91
105
  subject { either.join_left }
92
- let(:either) { described_class.new(Fear::Left.new('error')) }
106
+ let(:either) { described_class.new(Left('error')) }
93
107
 
94
108
  it { is_expected.to eq(either) }
95
109
  end
@@ -2,7 +2,7 @@ RSpec.describe Fear::Some do
2
2
  include Fear::Option::Mixin
3
3
 
4
4
  it_behaves_like Fear::RightBiased::Right do
5
- let(:right) { described_class.new('value') }
5
+ let(:right) { Some('value') }
6
6
  end
7
7
 
8
8
  subject(:some) { Some(42) }
@@ -1,5 +1,5 @@
1
1
  RSpec.describe Fear::Success do
2
- let(:success) { described_class.new('value') }
2
+ let(:success) { Success('value') }
3
3
 
4
4
  it_behaves_like Fear::RightBiased::Right do
5
5
  let(:right) { success }
@@ -29,6 +29,11 @@ RSpec.describe Fear::Success do
29
29
  it { is_expected.to be_success }
30
30
  end
31
31
 
32
+ describe '#failure?' do
33
+ subject { success }
34
+ it { is_expected.not_to be_failure }
35
+ end
36
+
32
37
  describe '#or_else' do
33
38
  subject { success.or_else { described_class.new('another value') } }
34
39
  it { is_expected.to eq(success) }
@@ -83,6 +88,6 @@ RSpec.describe Fear::Success do
83
88
 
84
89
  describe '#to_either' do
85
90
  subject { success.to_either }
86
- it { is_expected.to eq(Fear::Right.new('value')) }
91
+ it { is_expected.to eq(Right('value')) }
87
92
  end
88
93
  end
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,7 @@ require File.expand_path('spec/fear/right_biased/right')
6
6
  require File.expand_path('spec/fear/right_biased/left')
7
7
 
8
8
  RSpec.configure do |config|
9
+ config.include Fear::Mixin
9
10
  # rspec-expectations config goes here. You can use an alternate
10
11
  # assertion/expectation library such as wrong or the stdlib/minitest
11
12
  # assertions if you prefer.
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.2.0
4
+ version: 0.3.0
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-10 00:00:00.000000000 Z
11
+ date: 2016-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-equalizer
@@ -94,6 +94,20 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: simplecov
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
97
111
  description: Ruby port of some Scala's monads.
98
112
  email:
99
113
  - abolshakov@spbtv.com