sums_up 1.1.0 → 1.2.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
  SHA256:
3
- metadata.gz: e2abe6b0d49df01a5e9c588600079c91e3927c81501eb7816a0e6615a0579e00
4
- data.tar.gz: 9d18cf08079f9508c53a41e8fc97707346de0e36ab2d27c55089322e61b84bed
3
+ metadata.gz: b738f32804f828e44cb44a935122653f979279de6d7b69b1818bd29aace58775
4
+ data.tar.gz: 7549c23eeb68eed7e11f3ac00e4ccab39f7f5efebea5fabc7c5422871ba3141e
5
5
  SHA512:
6
- metadata.gz: 3efb1c7fd3c3e0c92f17aa0f2d92fa344f802be872785836e401c5a9f88f1d090b135542c11cd9a23aa1b158c0072b789eee47bf56aae328198395358f08223b
7
- data.tar.gz: 74c83c4a9f6efa3ba3a6466da3f02f9a064af367ee59b94ba6566cb270f1d4a7cdeea6b54c8d9af1d60038accf258cbf9426bf3d262e3a623b0758e8313ded27
6
+ metadata.gz: '0459d08b4c857ce4fd23ec3fb8983591284519dc67aa65a9ba262467cb8e8d5749e4a47e9f29e39f55b68b49534ec80bd1c91e6b5c873d94c225779ee7b0f1de'
7
+ data.tar.gz: 85cb701af891be51103a3be3cef418f6419e656288d2fc764ef244b7a559091cb7e640e1042c3ae30def6c30a79432d418cb120c339f5983d5e4e4882d44086d
data/.rubocop.yml CHANGED
@@ -21,6 +21,9 @@ Metrics/BlockLength:
21
21
  Metrics/MethodLength:
22
22
  Enabled: false
23
23
 
24
+ Style/ExplicitBlockArgument:
25
+ Enabled: false
26
+
24
27
  # Disabling for now to support Ruby 2.3.
25
28
  Style/HashTransformValues:
26
29
  Enabled: false
data/README.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Sum types for Ruby with zero runtime dependencies. Inspired by [hojberg/sums-up](https://github.com/hojberg/sums-up).
4
4
 
5
- [![Build Status](https://travis-ci.org/nahiluhmot/sums_up.svg?branch=main)](https://travis-ci.org/nahiluhmot/sums_up)
5
+ [![Build Status](https://travis-ci.com/nahiluhmot/sums_up.svg?branch=main)](https://travis-ci.com/nahiluhmot/sums_up)
6
6
  [![Gem Version](https://badge.fury.io/rb/sums_up.svg)](https://badge.fury.io/rb/sums_up)
7
7
 
8
8
  * [What is a Sum Type?](#what-is-a-sum-type)
@@ -631,6 +631,23 @@ SumsUp::Maybe.of(false)
631
631
  # => #<variant SumsUp::Maybe::Just value=false>
632
632
  ```
633
633
 
634
+ `SumsUp::Maybe#chain` (and its alias, `#flat_map`) can compose blocks which return `SumsUp::Maybe`s, halting at the first `SumsUp::Maybe.nothing`:
635
+
636
+ ```ruby
637
+ def double_if_odd(n)
638
+ SumsUp::Maybe
639
+ .just(n)
640
+ .chain { |x| x.odd? ? SumsUp::Maybe.just(x) : SumsUp::Maybe.nothing }
641
+ .chain { |x| SumsUp::Maybe.just(x * 2) }
642
+ end
643
+
644
+ double_if_odd(3)
645
+ # => #<variant SumsUp::Maybe::Just value=6>
646
+
647
+ double_if_odd(42)
648
+ # => #<variant SumsUp::Maybe::Nothing>
649
+ ```
650
+
634
651
  `SumsUp::Maybe#map` applies a function to the value if it's present:
635
652
 
636
653
  ```ruby
@@ -712,6 +729,50 @@ SumsUp::Result.from_block { raise 'unexpected error' }
712
729
 
713
730
  SumsUp::Result.from_block { 'good result' }
714
731
  # => #<variant SumsUp::Result::Success value="good result">
732
+
733
+ SumsUp::Result.from_block(ArgumentError) { raise ArgumentError, 'bad argument' }
734
+ # => #<variant SumsUp::Result::Failure error=#<ArgumentError: Bad argument>
735
+
736
+ SumsUp::Result.from_block(ArgumentError) { raise KeyError, 'no such key' }
737
+ # => KeyError: no such key
738
+ ```
739
+
740
+ `SumsUp::Result#chain` (and its alias, `#flat_map`) can compose blocks which return `SumsUp::Result`s, halting at the first `SumsUp::Result.failure(...)`:
741
+
742
+ ```ruby
743
+ User = Struct.new(:name, :email)
744
+
745
+ def parse_user(hash)
746
+ fetch_key(hash, :name).chain do |name|
747
+ fetch_key(hash, :email).chain do |email|
748
+ if URI::MailTo::EMAIL_REGEXP.match?(email)
749
+ SumsUp::Result.success(User.new(name, email))
750
+ else
751
+ SumsUp::Result.failure("invalid email: #{email}")
752
+ end
753
+ end
754
+ end
755
+ end
756
+
757
+ def fetch_key(hash, key)
758
+ if hash.key?(key)
759
+ SumsUp::Result.success(hash[key])
760
+ else
761
+ SumsUp::Result.failure("key not found: #{key}")
762
+ end
763
+ end
764
+
765
+ parse_user(email: '1337h@ck.er')
766
+ # => #<variant SumsUp::Result::Failure error="key not found: name">
767
+
768
+ parse_user(name: 'Sam')
769
+ # => #<variant SumsUp::Result::Failure error="key not found: email">
770
+
771
+ parse_user(name: 'Sam', email: '1337h@ck.er')
772
+ # => #<variant SumsUp::Result::Success value=#<struct User name="Sam", email="1337h@ck.er">>
773
+
774
+ parse_user(name: 'Sam', email: '1337hacker')
775
+ # => #<variant SumsUp::Result::Failure error="invalid email: 1337hacker">
715
776
  ```
716
777
 
717
778
  `SumsUp::Result#map` applies a function to the successful values:
@@ -1,5 +1,12 @@
1
1
  # Changelog
2
2
 
3
+ ## vX.Y.Z
4
+
5
+ ## v1.2.0
6
+
7
+ * Define `SumsUp::{Maybe,Result}#{chain,flat_map}`
8
+ * `SumsUp::Result#from_block` now accepts an optional `Class` argument
9
+
3
10
  ## v1.1.0
4
11
 
5
12
  * Support rubies as low as 2.3
data/lib/sums_up/maybe.rb CHANGED
@@ -17,6 +17,15 @@ module SumsUp
17
17
  end
18
18
  end
19
19
 
20
+ def chain
21
+ match do |m|
22
+ m.just { |value| yield(value) }
23
+ m.nothing self
24
+ end
25
+ end
26
+
27
+ alias_method(:flat_map, :chain)
28
+
20
29
  # Map a function across the Maybe. If present, the value is yielded and that
21
30
  # result is wrapped in a new Maybe.just. Returns Maybe.nothing otherwise.
22
31
  def map
@@ -11,12 +11,21 @@ module SumsUp
11
11
  Result = SumsUp.define(failure: :error, success: :value) do
12
12
  # Yield, wrapping the result in Result.success, or wrap the raised error
13
13
  # in Result.failure.
14
- def self.from_block
14
+ def self.from_block(error_class = StandardError)
15
15
  success(yield)
16
- rescue StandardError => e
16
+ rescue error_class => e
17
17
  failure(e)
18
18
  end
19
19
 
20
+ def chain
21
+ match do |m|
22
+ m.success { |value| yield(value) }
23
+ m.failure self
24
+ end
25
+ end
26
+
27
+ alias_method(:flat_map, :chain)
28
+
20
29
  # Map a function across the successful value (if present).
21
30
  def map
22
31
  match do |m|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SumsUp
4
- VERSION = '1.1.0'
4
+ VERSION = '1.2.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sums_up
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Hulihan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-06-12 00:00:00.000000000 Z
11
+ date: 2021-07-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pry