mayhaps 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ad9d2a355737f9f8031116906dbb644e3a76929
4
- data.tar.gz: 27598791c1bf5ce7278b314a53190449de0da058
3
+ metadata.gz: 6b040f7ea36f59ace8e9eeff30c3d60dbdd10e56
4
+ data.tar.gz: bd7c93a5baa9debaf9cf5408f8ba5cec9fa3502f
5
5
  SHA512:
6
- metadata.gz: 7e5330cb5822377a2cecfff2c1609e5ef7a0ef4aa7f299ea5d9b870937ec3e055e56c0cf003564c7fb74154de0be1969c02b12dcf97047e9aebe815956710a49
7
- data.tar.gz: 552ad065e2b519f24138f5816f9dc124d2860db4a6050997f3462d3490ad897a2fa14fa9307fc4290f140b37f61426e292484ee0726fda36595a04bff8ecc46d
6
+ metadata.gz: 34ac68a02130ef8f656421ce90b91073131b977a90bde11b4fc505ac61bc43ace4fed87ce37df8733782dd822848130c7f40273e68bd0232526367877e9b50ae
7
+ data.tar.gz: 0792ea6963a010e375802c14c66d0aaab069ad16f7acacfc6946d5bb5445a089a5ce735cade6aaaa790c52c7d49eeaee1665a367035eb0046384d63b2fa02b91
@@ -0,0 +1 @@
1
+ *.gem
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
- # Mayhaps
1
+ # Mayhaps [![Gem Version](https://badge.fury.io/rb/mayhaps.png)](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.1.0"
13
+ gem "mayhaps", "~> 0.2.0"
14
14
  ```
15
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.
16
+ Next, require it:
30
17
 
31
18
  ```ruby
32
- hash = {:foo => "foo"}
33
-
34
- hash[:foo].maybe.upcase #=> "FOO"
35
- hash[:bar].maybe.upcase #=> nil
19
+ require 'mayhaps'
36
20
  ```
37
21
 
38
- ### Chaining
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
- 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.
30
+ #### Examples
44
31
 
45
32
  ```ruby
46
33
  hash = {:foo => " foo "}
47
34
 
48
- hash[:foo].maybe_chain.strip.upcase.end #=> "FOO"
49
- hash[:bar].maybe_chain.strip.upcase.end #=> nil
50
- ```
35
+ hash[:foo].maybe.upcase #=> #<Just " FOO ">
36
+ hash[:bar].maybe.upcase #=> #<Nothing>
51
37
 
52
- ### Examples
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/maybe'
52
+ require 'mayhaps'
62
53
 
63
- if customer.maybe_chain.order.id.end == some_id
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>
@@ -1,39 +1,54 @@
1
1
  require 'singleton'
2
2
 
3
- module Mayhaps
4
- class Nothing
5
- include Singleton
6
-
7
- def method_missing(*args)
8
- nil
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
- class Chain
13
- attr_reader :end
13
+ module Maybe
14
+ def maybe
15
+ self
16
+ end
17
+ end
14
18
 
15
- def initialize(obj)
16
- @end = obj
17
- end
19
+ class Just < BasicObject
20
+ include ::Maybe
18
21
 
19
- def method_missing(*args, &block)
20
- @end.nil? ? self : Chain.new(@end.public_send(*args, &block))
21
- end
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
- class Object
26
- def mayhaps
27
- self
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 mayhaps_chain
31
- Mayhaps::Chain.new(self)
35
+ def inspect
36
+ "#<Just #{@value.inspect}>"
32
37
  end
33
38
  end
34
39
 
35
- class NilClass
36
- def mayhaps
37
- Mayhaps::Nothing.instance
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
@@ -1,11 +1,11 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'mayhaps'
3
- s.version = '0.1.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.1.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-05 00:00:00.000000000 Z
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: []
@@ -1,6 +0,0 @@
1
- require 'mayhaps'
2
-
3
- class Object
4
- alias_method :maybe, :mayhaps
5
- alias_method :maybe_chain, :mayhaps_chain
6
- end