or_else 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +23 -19
- data/lib/or_else/just.rb +2 -3
- data/lib/or_else/maybe.rb +1 -0
- data/lib/or_else/version.rb +1 -1
- data/spec/or_else/just_spec.rb +9 -3
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: eecae4c515e30eb7444310ddad11f7decc47d06e
|
4
|
+
data.tar.gz: 8f8c822cdd62cb58b7fceff8b9b5b5734f02fcaf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4add6df6ad601b2290bb7dad6e259031a5d22ddef709cb28726df6b16344cdeae0bb7423d874aed45c98fe261b470657e693426e32c9632aa74bff16a5e5a04a
|
7
|
+
data.tar.gz: 00aad4d57d23adf625c2e804ad33c2e4d11eddc90097af1c9a39cb62a630b9c263b948783fb4153a24331903811b8c11dff4515b729f841da73f9e19882635c1
|
data/README.md
CHANGED
@@ -19,29 +19,33 @@ Or install it yourself as:
|
|
19
19
|
## Usage
|
20
20
|
|
21
21
|
```ruby
|
22
|
-
|
23
|
-
value
|
24
|
-
|
25
|
-
|
22
|
+
# Nothings
|
23
|
+
value = Maybe(nil) # => Nothing
|
24
|
+
Maybe(nil).empty? # => true
|
25
|
+
Maybe(nil).nil? # => true
|
26
|
+
Maybe(nil).exists? # => false
|
26
27
|
# Block is ignored
|
27
|
-
|
28
|
+
Maybe(nil).map { |v| v } # => Nothing
|
28
29
|
# Block is ignored
|
29
|
-
|
30
|
+
Maybe(nil).flat_map { |v| v } # => Nothing
|
30
31
|
# Returns the result of the block
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
value
|
35
|
-
value.
|
36
|
-
value.
|
37
|
-
#
|
38
|
-
value.map { |v| v }
|
39
|
-
|
40
|
-
value.flat_map { |v| Maybe(v) } # => <OrElse::Just> (Block is yielded the bare value)
|
41
|
-
# Block is yielded the bare value, but did not exit with a Maybe so it is wrapped
|
42
|
-
value.flat_map { |v| v } # => <OrElse::Just>
|
32
|
+
Maybe(nil).or_else { 'foo' } # => 'foo'
|
33
|
+
|
34
|
+
# Justs
|
35
|
+
Maybe('value') # => Just('value')
|
36
|
+
Maybe('value').empty? # => false
|
37
|
+
Maybe('value').nil? # => false
|
38
|
+
Maybe('value').exists? # => true
|
39
|
+
Maybe('value').map { |v| v } # => Just('value')
|
40
|
+
Maybe('value').flat_map { |v| v } # => Maybe('value')
|
43
41
|
# Block is ignored
|
44
|
-
value.or_else { 'foo' }
|
42
|
+
Maybe('value').or_else { 'foo' } # => 'value'
|
43
|
+
|
44
|
+
#more examples
|
45
|
+
Maybe(1).map { |v| nil } # => Just(nil)
|
46
|
+
Maybe(1).map { |v| Maybe(v) } # => Just(Maybe(1))
|
47
|
+
Maybe(1).flat_map { |v| nil } # => Nothing
|
48
|
+
Maybe(1).flat_map { |v| Maybe(v) } # => Maybe(1)
|
45
49
|
```
|
46
50
|
|
47
51
|
Real life example:
|
data/lib/or_else/just.rb
CHANGED
data/lib/or_else/maybe.rb
CHANGED
data/lib/or_else/version.rb
CHANGED
data/spec/or_else/just_spec.rb
CHANGED
@@ -28,12 +28,12 @@ module OrElse
|
|
28
28
|
|
29
29
|
specify { expect { |b| just.map(&b) }.to yield_with_args(val) }
|
30
30
|
|
31
|
-
it 'yields the value 1 and wraps the block in a
|
31
|
+
it 'yields the value 1 and wraps the block in a Just' do
|
32
32
|
expect(just.map { |j| j }.value).to eq val
|
33
33
|
end
|
34
34
|
|
35
|
-
it 'yields the value 1 and is
|
36
|
-
expect(just.map { |j| nil }).to eq
|
35
|
+
it 'yields the value 1 and is Just(nil) when the block evaluates to nil' do
|
36
|
+
expect(just.map { |j| nil }.value).to eq nil
|
37
37
|
end
|
38
38
|
end
|
39
39
|
|
@@ -61,6 +61,12 @@ module OrElse
|
|
61
61
|
it 'wraps the result in a Maybe' do
|
62
62
|
expect(just.flat_map { |j| j }.value).to eq val
|
63
63
|
end
|
64
|
+
|
65
|
+
context 'when the block exists with a nil' do
|
66
|
+
it 'returns Nothing' do
|
67
|
+
expect(just.flat_map { |j| nil }).to eq Nothing
|
68
|
+
end
|
69
|
+
end
|
64
70
|
end
|
65
71
|
end
|
66
72
|
|