just_maybe 1.0.1 → 1.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +13 -6
- data/lib/just_maybe/version.rb +1 -1
- 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: 197a32d73ce562bdb84b5407ab9103c3feaafe1d
|
4
|
+
data.tar.gz: c2e49cd9197f0da89a019ef356039f7ab15f08b4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dd72c9967ee2e05138a426444d5d988c8ebf8351372a5be7ce1536bf15934f2950611ab32cd20bacd4443d5caf4c73722173999765bcbfb15433b7c9b1348832
|
7
|
+
data.tar.gz: 29ad284e103668974a657d527295dce2be6483875e612b683c26b8a72a3c3e9a25cac7872fab45d5828262c82b62c13b5cda1ec90e6042d5f88ace12537f42c9
|
data/README.md
CHANGED
@@ -2,11 +2,13 @@
|
|
2
2
|
|
3
3
|
An implementation of the "Maybe" pattern.
|
4
4
|
|
5
|
-
The philosophy
|
5
|
+
The philosophy behind this implementation of `Maybe` is to wrap optional values at the point of first contact.
|
6
6
|
|
7
|
-
|
7
|
+
So for example, instead of allowing `Person.find` to return `nil` you return a `Maybe` wrapping either a `Person` or `nil`. This forces consumers of `Person.find` to access the underlying value with `try`, thus explicitly acknowledging the returned values could be a `Person` or `Nothing`.
|
8
8
|
|
9
|
-
|
9
|
+
The implementation is extremely simple, does no monkey patching and does not rely on method missing.
|
10
|
+
|
11
|
+
It has a tiny API, providing three methods `nothing?`, `something?` and `try`.
|
10
12
|
|
11
13
|
|
12
14
|
## Installation
|
@@ -47,10 +49,10 @@ Maybe('anything')
|
|
47
49
|
# => <Just:0x000000020e04f0 @object="anything">
|
48
50
|
|
49
51
|
# Calling Maybe(nil) returns an instance of Nothing
|
50
|
-
Maybe(
|
52
|
+
Maybe(nil)
|
51
53
|
# => <Nothing:0x000000020b1718>
|
52
54
|
|
53
|
-
# Instances of Just and Nothing respond to the
|
55
|
+
# Instances of Just and Nothing respond to the predicate methods nothing? and something?
|
54
56
|
Maybe(42).nothing? # => false
|
55
57
|
Maybe(42).something? # => true
|
56
58
|
Maybe(nil).nothing? # => true
|
@@ -62,12 +64,17 @@ Maybe(40).try{|m| m.succ.succ } # => 42
|
|
62
64
|
|
63
65
|
# When try is called on instances of Nothing it returns nil
|
64
66
|
Maybe(nil).try{|m| m.succ.succ } # => nil
|
67
|
+
|
68
|
+
# A common pattern would be to use try || default
|
69
|
+
Maybe(41).try{|m| m.succ } || 0 # 42
|
70
|
+
Maybe(nil).try{|m| m.succ } || 0 # 0
|
65
71
|
```
|
66
72
|
|
67
73
|
|
74
|
+
|
68
75
|
## Notice
|
69
76
|
|
70
|
-
The public
|
77
|
+
The public interface of this gem was radically changed with the jump to version 1.0. If you were using version `0.2` or earlier you should pin the version in your Gemfile at "~> 0.2"
|
71
78
|
|
72
79
|
## Related
|
73
80
|
* [Null Objects and Falsiness](http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/)
|
data/lib/just_maybe/version.rb
CHANGED