mayhaps 0.1.0 → 0.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/.gitignore +1 -0
- data/README.md +66 -32
- data/lib/mayhaps.rb +38 -23
- data/mayhaps.gemspec +2 -2
- metadata +5 -5
- data/lib/mayhaps/maybe.rb +0 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6b040f7ea36f59ace8e9eeff30c3d60dbdd10e56
|
4
|
+
data.tar.gz: bd7c93a5baa9debaf9cf5408f8ba5cec9fa3502f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 34ac68a02130ef8f656421ce90b91073131b977a90bde11b4fc505ac61bc43ace4fed87ce37df8733782dd822848130c7f40273e68bd0232526367877e9b50ae
|
7
|
+
data.tar.gz: 0792ea6963a010e375802c14c66d0aaab069ad16f7acacfc6946d5bb5445a089a5ce735cade6aaaa790c52c7d49eeaee1665a367035eb0046384d63b2fa02b91
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# Mayhaps
|
1
|
+
# Mayhaps [](http://badge.fury.io/rb/mayhaps)
|
2
2
|
|
3
|
-
Mayhaps call Ruby methods.
|
3
|
+
Mayhaps (maybe) call Ruby methods.
|
4
4
|
|
5
5
|
## Usage
|
6
6
|
|
@@ -10,46 +10,37 @@ nil.
|
|
10
10
|
First, install the gem or add it to your `Gemfile`:
|
11
11
|
|
12
12
|
```ruby
|
13
|
-
gem "mayhaps", "~> 0.
|
13
|
+
gem "mayhaps", "~> 0.2.0"
|
14
14
|
```
|
15
15
|
|
16
|
-
Next, require it
|
17
|
-
|
18
|
-
1. `require 'mayhaps'`: Provides the `mayhaps` and `mayhaps_chain`
|
19
|
-
methods.
|
20
|
-
2. `require 'mayhaps/maybe'`: Provides the `maybe` and `maybe_chain`
|
21
|
-
methods as aliases for the above.
|
22
|
-
|
23
|
-
For the following examples, `require 'mayhaps/maybe'` is used.
|
24
|
-
|
25
|
-
### Maybe
|
26
|
-
|
27
|
-
The `maybe` method allows you to call a method on an object that may or
|
28
|
-
may not be nil. If the object is nil, then the result of calling the
|
29
|
-
method will also be nil.
|
16
|
+
Next, require it:
|
30
17
|
|
31
18
|
```ruby
|
32
|
-
|
33
|
-
|
34
|
-
hash[:foo].maybe.upcase #=> "FOO"
|
35
|
-
hash[:bar].maybe.upcase #=> nil
|
19
|
+
require 'mayhaps'
|
36
20
|
```
|
37
21
|
|
38
|
-
###
|
22
|
+
### On the surface
|
23
|
+
|
24
|
+
Mayhaps adds a `maybe` method to all objects that allows you to call
|
25
|
+
methods on the object regardless of whether or not it is nil. It does
|
26
|
+
this by wrapping the object in a `Maybe` object, more specifically a
|
27
|
+
`Nothing` object for nil or a `Just` object for anything else. To unwrap
|
28
|
+
the value, use unary `+` on the `Maybe` object.
|
39
29
|
|
40
|
-
|
41
|
-
method calls and requires you to call `end` in order to retrieve the
|
42
|
-
result, which is either the result of the chain of method calls, or nil
|
43
|
-
if any of the methods in the chain returned nil.
|
30
|
+
#### Examples
|
44
31
|
|
45
32
|
```ruby
|
46
33
|
hash = {:foo => " foo "}
|
47
34
|
|
48
|
-
hash[:foo].
|
49
|
-
hash[:bar].
|
50
|
-
```
|
35
|
+
hash[:foo].maybe.upcase #=> #<Just " FOO ">
|
36
|
+
hash[:bar].maybe.upcase #=> #<Nothing>
|
51
37
|
|
52
|
-
|
38
|
+
+hash[:foo].maybe.upcase #=> " FOO "
|
39
|
+
+hash[:bar].maybe.upcase #=> nil
|
40
|
+
|
41
|
+
+hash[:foo].maybe.upcase.strip #=> "FOO"
|
42
|
+
+hash[:bar].maybe.upcase.strip #=> nil
|
43
|
+
```
|
53
44
|
|
54
45
|
```ruby
|
55
46
|
# Without Mayhaps
|
@@ -58,13 +49,56 @@ if customer && customer.order && customer.order.id == some_id
|
|
58
49
|
end
|
59
50
|
|
60
51
|
# With Mayhaps
|
61
|
-
require 'mayhaps
|
52
|
+
require 'mayhaps'
|
62
53
|
|
63
|
-
if customer.
|
54
|
+
if +customer.maybe.order.id == some_id
|
64
55
|
# Do something with customer
|
65
56
|
end
|
66
57
|
```
|
67
58
|
|
59
|
+
### The nitty gritty
|
60
|
+
|
61
|
+
`Just` and `Nothing` both inherit from `BasicObject` rather than
|
62
|
+
`Object` in order to allow the maximum number of methods to fall through
|
63
|
+
to the wrapped object. This includes methods like `class` and `is_a?`,
|
64
|
+
therefore the `===` method of the `Maybe` classes should be used to
|
65
|
+
check if an object is wrapped in a `Maybe`.
|
66
|
+
|
67
|
+
```ruby
|
68
|
+
Maybe === :a.maybe #=> true
|
69
|
+
Just === :a.maybe #=> true
|
70
|
+
Maybe === nil.maybe #=> true
|
71
|
+
Nothing === nil.maybe #=> true
|
72
|
+
```
|
73
|
+
|
74
|
+
The methods that will be called on the `Maybe` object itself and not the
|
75
|
+
wrapped object are:
|
76
|
+
|
77
|
+
* `+@`
|
78
|
+
* `method_missing`
|
79
|
+
* `inspect`
|
80
|
+
* `maybe`
|
81
|
+
* `_dump`
|
82
|
+
* `==`
|
83
|
+
* `equal?`
|
84
|
+
* `!`
|
85
|
+
* `!=`
|
86
|
+
* `instance_eval`
|
87
|
+
* `instance_exec`
|
88
|
+
* `__send__`
|
89
|
+
* `__id__`
|
90
|
+
|
91
|
+
`_dump` is a method added by the `Singleton` module, which `Nothing`
|
92
|
+
includes.
|
93
|
+
|
94
|
+
The `maybe` method defined on `Maybe` objects simply returns `self`.
|
95
|
+
|
96
|
+
The `maybe` method should be used to wrap objects in `Maybe` objects,
|
97
|
+
but they can also be created directly. Since `Nothing` is a `Singleton`,
|
98
|
+
its instance can be accessed using `Nothing.instance`. A `Just` object
|
99
|
+
can be created by calling `Just.new`. If the object passed to `Just.new`
|
100
|
+
is `nil`, an ArgumentError will be raised.
|
101
|
+
|
68
102
|
## License
|
69
103
|
|
70
104
|
Copyright © 2013, Curtis McEnroe <programble@gmail.com>
|
data/lib/mayhaps.rb
CHANGED
@@ -1,39 +1,54 @@
|
|
1
1
|
require 'singleton'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
class Object
|
4
|
+
def maybe
|
5
|
+
if nil?
|
6
|
+
Nothing.instance
|
7
|
+
else
|
8
|
+
Just.new(self)
|
9
9
|
end
|
10
10
|
end
|
11
|
+
end
|
11
12
|
|
12
|
-
|
13
|
-
|
13
|
+
module Maybe
|
14
|
+
def maybe
|
15
|
+
self
|
16
|
+
end
|
17
|
+
end
|
14
18
|
|
15
|
-
|
16
|
-
|
17
|
-
end
|
19
|
+
class Just < BasicObject
|
20
|
+
include ::Maybe
|
18
21
|
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
+
def initialize(obj)
|
23
|
+
::Kernel.raise ::ArgumentError, 'object is nil' if obj.nil?
|
24
|
+
@value = obj
|
22
25
|
end
|
23
|
-
end
|
24
26
|
|
25
|
-
|
26
|
-
|
27
|
-
|
27
|
+
def +@
|
28
|
+
@value
|
29
|
+
end
|
30
|
+
|
31
|
+
def method_missing(*args, &block)
|
32
|
+
@value.public_send(*args, &block).maybe
|
28
33
|
end
|
29
34
|
|
30
|
-
def
|
31
|
-
|
35
|
+
def inspect
|
36
|
+
"#<Just #{@value.inspect}>"
|
32
37
|
end
|
33
38
|
end
|
34
39
|
|
35
|
-
class
|
36
|
-
|
37
|
-
|
40
|
+
class Nothing < BasicObject
|
41
|
+
include ::Maybe, ::Singleton
|
42
|
+
|
43
|
+
def +@
|
44
|
+
nil
|
45
|
+
end
|
46
|
+
|
47
|
+
def method_missing(*args)
|
48
|
+
self
|
49
|
+
end
|
50
|
+
|
51
|
+
def inspect
|
52
|
+
'#<Nothing>'
|
38
53
|
end
|
39
54
|
end
|
data/mayhaps.gemspec
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
Gem::Specification.new do |s|
|
2
2
|
s.name = 'mayhaps'
|
3
|
-
s.version = '0.
|
3
|
+
s.version = '0.2.0'
|
4
4
|
s.license = 'ISC'
|
5
5
|
s.authors = ['Curtis McEnroe']
|
6
6
|
s.email = ['programble@gmail.com']
|
7
7
|
s.homepage = 'https://github.com/programble/mayhaps'
|
8
|
-
s.summary = 'Mayhaps call Ruby methods'
|
8
|
+
s.summary = 'Mayhaps (maybe) call Ruby methods'
|
9
9
|
s.description = s.summary
|
10
10
|
|
11
11
|
s.files = `git ls-files`.split("\n")
|
metadata
CHANGED
@@ -1,25 +1,25 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mayhaps
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Curtis McEnroe
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-10-
|
11
|
+
date: 2013-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
|
-
description: Mayhaps call Ruby methods
|
13
|
+
description: Mayhaps (maybe) call Ruby methods
|
14
14
|
email:
|
15
15
|
- programble@gmail.com
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
18
18
|
extra_rdoc_files: []
|
19
19
|
files:
|
20
|
+
- .gitignore
|
20
21
|
- README.md
|
21
22
|
- lib/mayhaps.rb
|
22
|
-
- lib/mayhaps/maybe.rb
|
23
23
|
- mayhaps.gemspec
|
24
24
|
homepage: https://github.com/programble/mayhaps
|
25
25
|
licenses:
|
@@ -44,5 +44,5 @@ rubyforge_project:
|
|
44
44
|
rubygems_version: 2.0.2
|
45
45
|
signing_key:
|
46
46
|
specification_version: 4
|
47
|
-
summary: Mayhaps call Ruby methods
|
47
|
+
summary: Mayhaps (maybe) call Ruby methods
|
48
48
|
test_files: []
|