mayhaps 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.md +82 -0
- data/lib/mayhaps.rb +39 -0
- data/lib/mayhaps/maybe.rb +6 -0
- data/mayhaps.gemspec +13 -0
- metadata +48 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2ad9d2a355737f9f8031116906dbb644e3a76929
|
4
|
+
data.tar.gz: 27598791c1bf5ce7278b314a53190449de0da058
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 7e5330cb5822377a2cecfff2c1609e5ef7a0ef4aa7f299ea5d9b870937ec3e055e56c0cf003564c7fb74154de0be1969c02b12dcf97047e9aebe815956710a49
|
7
|
+
data.tar.gz: 552ad065e2b519f24138f5816f9dc124d2860db4a6050997f3462d3490ad897a2fa14fa9307fc4290f140b37f61426e292484ee0726fda36595a04bff8ecc46d
|
data/README.md
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
# Mayhaps
|
2
|
+
|
3
|
+
Mayhaps call Ruby methods.
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
Mayhaps allows you to call methods on objects that may or may not be
|
8
|
+
nil.
|
9
|
+
|
10
|
+
First, install the gem or add it to your `Gemfile`:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem "mayhaps", "~> 0.1.0"
|
14
|
+
```
|
15
|
+
|
16
|
+
Next, require it. There are two choices for using Mayhaps:
|
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.
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
hash = {:foo => "foo"}
|
33
|
+
|
34
|
+
hash[:foo].maybe.upcase #=> "FOO"
|
35
|
+
hash[:bar].maybe.upcase #=> nil
|
36
|
+
```
|
37
|
+
|
38
|
+
### Chaining
|
39
|
+
|
40
|
+
The `maybe_chain` method works similarly, except it allows you to chain
|
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.
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
hash = {:foo => " foo "}
|
47
|
+
|
48
|
+
hash[:foo].maybe_chain.strip.upcase.end #=> "FOO"
|
49
|
+
hash[:bar].maybe_chain.strip.upcase.end #=> nil
|
50
|
+
```
|
51
|
+
|
52
|
+
### Examples
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
# Without Mayhaps
|
56
|
+
if customer && customer.order && customer.order.id == some_id
|
57
|
+
# Do something with customer
|
58
|
+
end
|
59
|
+
|
60
|
+
# With Mayhaps
|
61
|
+
require 'mayhaps/maybe'
|
62
|
+
|
63
|
+
if customer.maybe_chain.order.id.end == some_id
|
64
|
+
# Do something with customer
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
## License
|
69
|
+
|
70
|
+
Copyright © 2013, Curtis McEnroe <programble@gmail.com>
|
71
|
+
|
72
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
73
|
+
purpose with or without fee is hereby granted, provided that the above
|
74
|
+
copyright notice and this permission notice appear in all copies.
|
75
|
+
|
76
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
77
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
78
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
79
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
80
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
81
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
82
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
data/lib/mayhaps.rb
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
|
3
|
+
module Mayhaps
|
4
|
+
class Nothing
|
5
|
+
include Singleton
|
6
|
+
|
7
|
+
def method_missing(*args)
|
8
|
+
nil
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class Chain
|
13
|
+
attr_reader :end
|
14
|
+
|
15
|
+
def initialize(obj)
|
16
|
+
@end = obj
|
17
|
+
end
|
18
|
+
|
19
|
+
def method_missing(*args, &block)
|
20
|
+
@end.nil? ? self : Chain.new(@end.public_send(*args, &block))
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Object
|
26
|
+
def mayhaps
|
27
|
+
self
|
28
|
+
end
|
29
|
+
|
30
|
+
def mayhaps_chain
|
31
|
+
Mayhaps::Chain.new(self)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
class NilClass
|
36
|
+
def mayhaps
|
37
|
+
Mayhaps::Nothing.instance
|
38
|
+
end
|
39
|
+
end
|
data/mayhaps.gemspec
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'mayhaps'
|
3
|
+
s.version = '0.1.0'
|
4
|
+
s.license = 'ISC'
|
5
|
+
s.authors = ['Curtis McEnroe']
|
6
|
+
s.email = ['programble@gmail.com']
|
7
|
+
s.homepage = 'https://github.com/programble/mayhaps'
|
8
|
+
s.summary = 'Mayhaps call Ruby methods'
|
9
|
+
s.description = s.summary
|
10
|
+
|
11
|
+
s.files = `git ls-files`.split("\n")
|
12
|
+
s.require_paths = ['lib']
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mayhaps
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Curtis McEnroe
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-10-05 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: Mayhaps call Ruby methods
|
14
|
+
email:
|
15
|
+
- programble@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- README.md
|
21
|
+
- lib/mayhaps.rb
|
22
|
+
- lib/mayhaps/maybe.rb
|
23
|
+
- mayhaps.gemspec
|
24
|
+
homepage: https://github.com/programble/mayhaps
|
25
|
+
licenses:
|
26
|
+
- ISC
|
27
|
+
metadata: {}
|
28
|
+
post_install_message:
|
29
|
+
rdoc_options: []
|
30
|
+
require_paths:
|
31
|
+
- lib
|
32
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
33
|
+
requirements:
|
34
|
+
- - '>='
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '0'
|
37
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- - '>='
|
40
|
+
- !ruby/object:Gem::Version
|
41
|
+
version: '0'
|
42
|
+
requirements: []
|
43
|
+
rubyforge_project:
|
44
|
+
rubygems_version: 2.0.2
|
45
|
+
signing_key:
|
46
|
+
specification_version: 4
|
47
|
+
summary: Mayhaps call Ruby methods
|
48
|
+
test_files: []
|