just_maybe 0.1.2 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +17 -13
- data/lib/just_maybe/maybe.rb +15 -25
- data/lib/just_maybe/version.rb +1 -1
- data/spec/maybe_spec.rb +8 -4
- metadata +3 -3
data/README.md
CHANGED
@@ -16,7 +16,7 @@ Maybe(nil).foo.bar.or_else { 'default' }
|
|
16
16
|
|
17
17
|
Add this line to your application's Gemfile:
|
18
18
|
|
19
|
-
gem "just_maybe", "~> 0.
|
19
|
+
gem "just_maybe", "~> 0.2.0"
|
20
20
|
|
21
21
|
And then execute:
|
22
22
|
|
@@ -44,22 +44,22 @@ require 'just_maybe'
|
|
44
44
|
|
45
45
|
# Calling Maybe(object) returns that object wrapped in a instance of Maybe
|
46
46
|
Maybe(42)
|
47
|
-
# => #<Maybe:0x000000010e7c88
|
47
|
+
# => #<Maybe:0x000000010e7c88 @value=42>
|
48
48
|
|
49
49
|
# An instance of maybe will pass all valid messages to the underlying value
|
50
50
|
# and wrap the result in a new instance Maybe
|
51
51
|
Maybe(42).next
|
52
|
-
# => <Maybe:0x0000000110b0e8
|
52
|
+
# => <Maybe:0x0000000110b0e8 @value=43>
|
53
53
|
|
54
54
|
# If the Maybe's underlying value doesn't respond to the message it returns
|
55
55
|
# nil wrapped in a new instance of Maybe
|
56
56
|
Maybe(42).foo
|
57
|
-
# => #<Maybe:0x00000001113540
|
57
|
+
# => #<Maybe:0x00000001113540 @value=nil>
|
58
58
|
|
59
59
|
# This allows you to make arbitrarily long method chains with no risk of raising
|
60
60
|
# NoMethodError on a nil object
|
61
|
-
Maybe(nil).foo.bar.baz
|
62
|
-
# => #<Maybe:0x00000001120830
|
61
|
+
Maybe(nil).foo.bar.baz
|
62
|
+
# => #<Maybe:0x00000001120830 @value=nil>
|
63
63
|
|
64
64
|
# Finally Maybe responds to #or_else { ... }. This causes the Maybe to return the
|
65
65
|
# underlying value if it exists or the result of the block if it does not.
|
@@ -77,15 +77,19 @@ Maybe(nil).foo.bar.or_s # => ''
|
|
77
77
|
|
78
78
|
# Remember if you don't call or_else or one of it's alternatives you will get back
|
79
79
|
# an instance of Maybe. It is possible to get the underlying value back by calling
|
80
|
-
#
|
81
|
-
Maybe(42).
|
82
|
-
|
80
|
+
# __object__ but I'd generally consider using this a code smell.
|
81
|
+
Maybe(42).__object__
|
82
|
+
# => 42
|
83
|
+
Maybe(42).foo.bar.baz.__object__
|
84
|
+
# => nil
|
83
85
|
```
|
84
86
|
## Related
|
85
|
-
* [
|
86
|
-
* [
|
87
|
-
* [
|
88
|
-
* [
|
87
|
+
* [Null Objects and Falsiness](http://devblog.avdi.org/2011/05/30/null-objects-and-falsiness/)
|
88
|
+
* [If you gaze into nil, nil gazes also into you](http://robots.thoughtbot.com/post/8181879506/if-you-gaze-into-nil-nil-gazes-also-into-you)
|
89
|
+
* [The Maybe Monad In Ruby](http://lostechies.com/derickbailey/2010/10/10/the-maybe-monad-in-ruby/)
|
90
|
+
* [Mondadic](https://github.com/pzol/monadic)
|
91
|
+
* [Rumonade](https://github.com/ms-ati/rumonade)
|
92
|
+
* [Wrapped](https://github.com/mike-burns/wrapped)
|
89
93
|
|
90
94
|
## Contributing
|
91
95
|
|
data/lib/just_maybe/maybe.rb
CHANGED
@@ -1,22 +1,18 @@
|
|
1
1
|
class Maybe < BasicObject
|
2
2
|
def initialize(object)
|
3
|
-
|
4
|
-
@object = object.
|
5
|
-
|
3
|
+
begin
|
4
|
+
@object = object.__object__
|
5
|
+
rescue
|
6
6
|
@object = object
|
7
|
-
end
|
8
|
-
end
|
9
|
-
|
10
|
-
def instance_of?(klass)
|
11
|
-
::Maybe == klass
|
7
|
+
end
|
12
8
|
end
|
13
9
|
|
14
|
-
def
|
10
|
+
def __object__
|
15
11
|
@object
|
16
12
|
end
|
17
13
|
|
18
14
|
def inspect
|
19
|
-
::Kernel.sprintf('#<Maybe:0x%0.14x
|
15
|
+
::Kernel.sprintf('#<Maybe:0x%0.14x @object=%s>', __id__ * 2, @object.inspect)
|
20
16
|
end
|
21
17
|
|
22
18
|
def method_missing(method, *args, &block)
|
@@ -27,39 +23,33 @@ class Maybe < BasicObject
|
|
27
23
|
end
|
28
24
|
end
|
29
25
|
|
30
|
-
def or_a
|
31
|
-
return @object unless @object.nil?
|
32
|
-
[]
|
33
|
-
end
|
34
|
-
|
35
26
|
def or_else(&block)
|
36
27
|
return @object unless @object.nil?
|
37
28
|
yield
|
38
29
|
end
|
39
30
|
|
31
|
+
def or_a
|
32
|
+
or_else { [] }
|
33
|
+
end
|
34
|
+
|
40
35
|
def or_f
|
41
|
-
|
42
|
-
0.0
|
36
|
+
or_else { 0.0 }
|
43
37
|
end
|
44
38
|
|
45
39
|
def or_h
|
46
|
-
|
47
|
-
{}
|
40
|
+
or_else { {} }
|
48
41
|
end
|
49
42
|
|
50
43
|
def or_i
|
51
|
-
|
52
|
-
0
|
44
|
+
or_else { 0 }
|
53
45
|
end
|
54
46
|
|
55
47
|
def or_nil
|
56
|
-
|
57
|
-
nil
|
48
|
+
or_else { nil }
|
58
49
|
end
|
59
50
|
|
60
51
|
def or_s
|
61
|
-
|
62
|
-
''
|
52
|
+
or_else { '' }
|
63
53
|
end
|
64
54
|
end
|
65
55
|
|
data/lib/just_maybe/version.rb
CHANGED
data/spec/maybe_spec.rb
CHANGED
@@ -3,21 +3,25 @@ require 'just_maybe/maybe'
|
|
3
3
|
describe Maybe do
|
4
4
|
let(:object) { double }
|
5
5
|
|
6
|
+
before do
|
7
|
+
object.stub(:__object__).and_raise(NoMethodError)
|
8
|
+
end
|
9
|
+
|
6
10
|
it "delegates messages to wrapped object" do
|
7
11
|
object.should_receive(:foo).with('bar')
|
8
12
|
Maybe(object).foo('bar')
|
9
13
|
end
|
10
14
|
|
11
15
|
it "calling methods that don't exist return Maybe(nil)" do
|
12
|
-
Maybe(nil).foo.
|
16
|
+
Maybe(nil).foo.__object__.should be_nil
|
13
17
|
end
|
14
18
|
|
15
19
|
it "it doesn't nest" do
|
16
|
-
Maybe(Maybe(nil)).
|
20
|
+
Maybe(Maybe(nil)).__object__.should_not be_instance_of(Maybe)
|
17
21
|
end
|
18
22
|
|
19
|
-
it "
|
20
|
-
Maybe(object).
|
23
|
+
it "__object__ unwraps the underlying object" do
|
24
|
+
Maybe(object).__object__.should be(object)
|
21
25
|
end
|
22
26
|
|
23
27
|
it "#or_else returns then underlying object when it's not nil" do
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: just_maybe
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-06-
|
12
|
+
date: 2012-06-17 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
14
|
description: A simple implmenetation of the Maybe special case pattern
|
15
15
|
email:
|
@@ -48,7 +48,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
48
48
|
version: '0'
|
49
49
|
requirements: []
|
50
50
|
rubyforge_project:
|
51
|
-
rubygems_version: 1.8.
|
51
|
+
rubygems_version: 1.8.21
|
52
52
|
signing_key:
|
53
53
|
specification_version: 3
|
54
54
|
summary: A simple implmenetation of the Maybe special case pattern
|