maybe.rb 1.0.0 → 1.1.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/README.md +12 -0
- data/lib/maybe/maybe.rb +14 -0
- data/lib/maybe/pollute.rb +2 -2
- data/lib/maybe/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 76f25a1f8164ed416c85278d29854b79f9cc1b82
|
4
|
+
data.tar.gz: 72c7c898320b68f6cbfcaed4fce6c6075303dc90
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 45a77cd9f4ea97a2f64e054984cfc18b20a277328c37e2f30911b6fb659d16b14c1149da74df1cb28803aad4f1fd27e9b146e5f3f5624f26682888ae10d5f3cf
|
7
|
+
data.tar.gz: 92166879a1c7df591a153b34512506d982b62f1b0d728bdddc0ddec2337de797ad10c0a80ee5a52810419c2fd76dc021ee3183dae7abc1a7fbba5c8b8ba47dc7
|
data/README.md
CHANGED
@@ -57,6 +57,18 @@ This is disabled by default and the `Maybe` class itself doesn't rely on it and
|
|
57
57
|
because it's not too pleasant for a library to implicitly patch built-in Ruby
|
58
58
|
classes.
|
59
59
|
|
60
|
+
## Installation
|
61
|
+
|
62
|
+
Because both "maybe" and "ruby-maybe" were already taken this Gem is
|
63
|
+
conveniently called "maybe.rb" on RubyGems, this means you'll need to install it
|
64
|
+
as following:
|
65
|
+
|
66
|
+
gem install maybe.rb
|
67
|
+
|
68
|
+
Or if you're using Bundler:
|
69
|
+
|
70
|
+
gem 'maybe.rb'
|
71
|
+
|
60
72
|
## Usage
|
61
73
|
|
62
74
|
Load the Gem:
|
data/lib/maybe/maybe.rb
CHANGED
@@ -96,6 +96,16 @@ class Maybe
|
|
96
96
|
#
|
97
97
|
# maybe[0]['foo'].or(20)
|
98
98
|
#
|
99
|
+
# If both arguments and a block are given the supplied block is called with
|
100
|
+
# the value returned by the `#[]` method of the wrapped object. For example:
|
101
|
+
#
|
102
|
+
# maybe = Maybe.new(:number => '10')
|
103
|
+
#
|
104
|
+
# maybe.maybe(:number) { |number| number.to_i } # => 10
|
105
|
+
#
|
106
|
+
# The block in this case is _only_ called when the `#[]` method returns a non
|
107
|
+
# nil value.
|
108
|
+
#
|
99
109
|
# @param [Array] args
|
100
110
|
#
|
101
111
|
# @yieldparam [Mixed] wrapped The wrapped value
|
@@ -105,6 +115,10 @@ class Maybe
|
|
105
115
|
def maybe(*args)
|
106
116
|
if !args.empty?
|
107
117
|
value = brackets? ? @wrapped[*args] : nil
|
118
|
+
|
119
|
+
if block_given? and value
|
120
|
+
value = yield value
|
121
|
+
end
|
108
122
|
elsif @wrapped
|
109
123
|
value = yield @wrapped
|
110
124
|
end
|
data/lib/maybe/pollute.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
class Object
|
2
2
|
##
|
3
|
-
# Wraps the current object in a Maybe instance, optionally calling
|
4
|
-
# if an argument or block is given.
|
3
|
+
# Wraps the current object in a Maybe instance, optionally calling
|
4
|
+
# {Maybe#maybe} if an argument or block is given.
|
5
5
|
#
|
6
6
|
# @example
|
7
7
|
# [10, 20, 30].maybe(0).or(50) # => 10
|
data/lib/maybe/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: maybe.rb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Olery B.V.
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: yard
|