sums_up 1.1.0 → 1.2.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/.rubocop.yml +3 -0
- data/README.md +62 -1
- data/lib/sums_up/CHANGELOG.md +7 -0
- data/lib/sums_up/maybe.rb +9 -0
- data/lib/sums_up/result.rb +11 -2
- data/lib/sums_up/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: b738f32804f828e44cb44a935122653f979279de6d7b69b1818bd29aace58775
|
|
4
|
+
data.tar.gz: 7549c23eeb68eed7e11f3ac00e4ccab39f7f5efebea5fabc7c5422871ba3141e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0459d08b4c857ce4fd23ec3fb8983591284519dc67aa65a9ba262467cb8e8d5749e4a47e9f29e39f55b68b49534ec80bd1c91e6b5c873d94c225779ee7b0f1de'
|
|
7
|
+
data.tar.gz: 85cb701af891be51103a3be3cef418f6419e656288d2fc764ef244b7a559091cb7e640e1042c3ae30def6c30a79432d418cb120c339f5983d5e4e4882d44086d
|
data/.rubocop.yml
CHANGED
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
|
-
[](https://travis-ci.com/nahiluhmot/sums_up)
|
|
6
6
|
[](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:
|
data/lib/sums_up/CHANGELOG.md
CHANGED
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
|
data/lib/sums_up/result.rb
CHANGED
|
@@ -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
|
|
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|
|
data/lib/sums_up/version.rb
CHANGED
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.
|
|
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-
|
|
11
|
+
date: 2021-07-15 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: pry
|