fear 0.2.0 → 0.3.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 +5 -0
- data/fear.gemspec +1 -0
- data/lib/fear/either.rb +12 -0
- data/lib/fear/left.rb +5 -0
- data/lib/fear/right.rb +9 -0
- data/lib/fear/version.rb +1 -1
- data/spec/fear/failure_spec.rb +9 -4
- data/spec/fear/for_spec.rb +20 -22
- data/spec/fear/left_spec.rb +12 -2
- data/spec/fear/none_spec.rb +1 -1
- data/spec/fear/right_spec.rb +22 -8
- data/spec/fear/some_spec.rb +1 -1
- data/spec/fear/success_spec.rb +7 -2
- data/spec/spec_helper.rb +1 -0
- metadata +16 -2
    
        checksums.yaml
    CHANGED
    
    | @@ -1,7 +1,7 @@ | |
| 1 1 | 
             
            ---
         | 
| 2 2 | 
             
            SHA1:
         | 
| 3 | 
            -
              metadata.gz:  | 
| 4 | 
            -
              data.tar.gz:  | 
| 3 | 
            +
              metadata.gz: 3ab163e8a3ccc420c6c1436e42c269e36e333b36
         | 
| 4 | 
            +
              data.tar.gz: ac6d7a735f7c6ac3f20194438252b59fbdecf8c4
         | 
| 5 5 | 
             
            SHA512:
         | 
| 6 | 
            -
              metadata.gz:  | 
| 7 | 
            -
              data.tar.gz:  | 
| 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
    
    
    
        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
    
    
    
        data/lib/fear/right.rb
    CHANGED
    
    
    
        data/lib/fear/version.rb
    CHANGED
    
    
    
        data/spec/fear/failure_spec.rb
    CHANGED
    
    | @@ -1,6 +1,6 @@ | |
| 1 1 | 
             
            RSpec.describe Fear::Failure do
         | 
| 2 2 | 
             
              let(:exception) { RuntimeError.new('error') }
         | 
| 3 | 
            -
              let(:failure) {  | 
| 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 | 
            -
                       | 
| 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( | 
| 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( | 
| 87 | 
            +
                it { is_expected.to eq(Left(exception)) }
         | 
| 83 88 | 
             
              end
         | 
| 84 89 | 
             
            end
         | 
    
        data/spec/fear/for_spec.rb
    CHANGED
    
    | @@ -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:  | 
| 5 | 
            +
                    For(a: Some(2)) { a * 2 }
         | 
| 8 6 | 
             
                  end
         | 
| 9 7 |  | 
| 10 | 
            -
                  it { is_expected.to eq( | 
| 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:  | 
| 13 | 
            +
                    For(a: None()) { a * 2 }
         | 
| 16 14 | 
             
                  end
         | 
| 17 15 |  | 
| 18 | 
            -
                  it { is_expected.to eq( | 
| 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) {  | 
| 40 | 
            -
                  let(:second) {  | 
| 41 | 
            -
                  let(:third) {  | 
| 37 | 
            +
                  let(:first) { Some(2) }
         | 
| 38 | 
            +
                  let(:second) { Some(3) }
         | 
| 39 | 
            +
                  let(:third) { Some(4) }
         | 
| 42 40 |  | 
| 43 | 
            -
                  it { is_expected.to eq( | 
| 41 | 
            +
                  it { is_expected.to eq(Some(24)) }
         | 
| 44 42 | 
             
                end
         | 
| 45 43 |  | 
| 46 44 | 
             
                context 'first None' do
         | 
| 47 | 
            -
                  let(:first) {  | 
| 48 | 
            -
                  let(:second) {  | 
| 49 | 
            -
                  let(:third) {  | 
| 45 | 
            +
                  let(:first) { None() }
         | 
| 46 | 
            +
                  let(:second) { Some(3) }
         | 
| 47 | 
            +
                  let(:third) { Some(4) }
         | 
| 50 48 |  | 
| 51 | 
            -
                  it { is_expected.to eq( | 
| 49 | 
            +
                  it { is_expected.to eq(None()) }
         | 
| 52 50 | 
             
                end
         | 
| 53 51 |  | 
| 54 52 | 
             
                context 'second None' do
         | 
| 55 | 
            -
                  let(:first) {  | 
| 56 | 
            -
                  let(:second) {  | 
| 57 | 
            -
                  let(:third) {  | 
| 53 | 
            +
                  let(:first) { Some(2) }
         | 
| 54 | 
            +
                  let(:second) { None() }
         | 
| 55 | 
            +
                  let(:third) { Some(4) }
         | 
| 58 56 |  | 
| 59 | 
            -
                  it { is_expected.to eq( | 
| 57 | 
            +
                  it { is_expected.to eq(None()) }
         | 
| 60 58 | 
             
                end
         | 
| 61 59 |  | 
| 62 60 | 
             
                context 'last None' do
         | 
| 63 | 
            -
                  let(:first) {  | 
| 64 | 
            -
                  let(:second) {  | 
| 65 | 
            -
                  let(:third) {  | 
| 61 | 
            +
                  let(:first) { Some(2) }
         | 
| 62 | 
            +
                  let(:second) { Some(3) }
         | 
| 63 | 
            +
                  let(:third) { None() }
         | 
| 66 64 |  | 
| 67 | 
            -
                  it { is_expected.to eq( | 
| 65 | 
            +
                  it { is_expected.to eq(None()) }
         | 
| 68 66 | 
             
                end
         | 
| 69 67 | 
             
              end
         | 
| 70 68 | 
             
            end
         | 
    
        data/spec/fear/left_spec.rb
    CHANGED
    
    | @@ -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) {  | 
| 5 | 
            +
                let(:left) { Left('value') }
         | 
| 6 6 | 
             
              end
         | 
| 7 7 |  | 
| 8 | 
            -
              let(:left) {  | 
| 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')) }
         | 
    
        data/spec/fear/none_spec.rb
    CHANGED
    
    
    
        data/spec/fear/right_spec.rb
    CHANGED
    
    | @@ -1,9 +1,9 @@ | |
| 1 1 | 
             
            RSpec.describe Fear::Right do
         | 
| 2 2 | 
             
              it_behaves_like Fear::RightBiased::Right do
         | 
| 3 | 
            -
                let(:right) {  | 
| 3 | 
            +
                let(:right) { Right('value') }
         | 
| 4 4 | 
             
              end
         | 
| 5 5 |  | 
| 6 | 
            -
              let(:right) {  | 
| 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( | 
| 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( | 
| 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( | 
| 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( | 
| 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) {  | 
| 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( | 
| 106 | 
            +
                  let(:either) { described_class.new(Left('error')) }
         | 
| 93 107 |  | 
| 94 108 | 
             
                  it { is_expected.to eq(either) }
         | 
| 95 109 | 
             
                end
         | 
    
        data/spec/fear/some_spec.rb
    CHANGED
    
    
    
        data/spec/fear/success_spec.rb
    CHANGED
    
    | @@ -1,5 +1,5 @@ | |
| 1 1 | 
             
            RSpec.describe Fear::Success do
         | 
| 2 | 
            -
              let(:success) {  | 
| 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( | 
| 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. | 
| 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- | 
| 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
         |