mug 0.2.3 → 0.2.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mug/and-or.rb +48 -48
- data/lib/mug/apply.rb +58 -58
- data/lib/mug/bool.rb +128 -128
- data/lib/mug/fragile-method-chain.rb +70 -70
- data/lib/mug/iterator.rb +2 -2
- data/lib/mug/iterator/for.rb +29 -29
- data/lib/mug/iterator/method.rb +29 -29
- data/lib/mug/iterator_c.rb +44 -44
- data/lib/mug/maybe.rb +59 -59
- data/lib/mug/self.rb +13 -0
- data/lib/mug/tau.rb +44 -44
- data/test/test-and-or.rb +41 -41
- data/test/test-fragile-method-chain.rb +65 -65
- data/test/test-iterator-for.rb +18 -18
- data/test/test-iterator-method.rb +18 -18
- data/test/test-maybe.rb +90 -90
- data/test/test-self.rb +10 -0
- data/test/test-tau.rb +22 -22
- data/test/test-to_h.rb +16 -16
- data/test/test_apply.rb +56 -56
- metadata +19 -18
@@ -1,70 +1,70 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
# Invokes a method chain until one method returns a falsy value.
|
4
|
-
#
|
5
|
-
# For example:
|
6
|
-
#
|
7
|
-
# a._?.b.c.d._!
|
8
|
-
# nested_hash._?[:a][:b][:c]._!
|
9
|
-
#
|
10
|
-
class FragileMethodChain
|
11
|
-
#
|
12
|
-
# Creates a FragileMethodChain which will send its first method to +o+
|
13
|
-
#
|
14
|
-
def initialize o
|
15
|
-
@o = o
|
16
|
-
@chain = []
|
17
|
-
end
|
18
|
-
|
19
|
-
#
|
20
|
-
# Finalises the FragileMethodChain.
|
21
|
-
#
|
22
|
-
# The final result will be the first +nil+ or +false+ value
|
23
|
-
# returned in the chain, or its end result.
|
24
|
-
#
|
25
|
-
def _!
|
26
|
-
@chain.inject(@o) do |o,x|
|
27
|
-
a,b = x
|
28
|
-
break o unless o
|
29
|
-
o.__send__(*a, &b)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
# Record the method args/block
|
34
|
-
def method_missing *a, &b #:nodoc:
|
35
|
-
@chain << [a,b]
|
36
|
-
self
|
37
|
-
end
|
38
|
-
|
39
|
-
# Explicitly record :_? as a method in the chain.
|
40
|
-
def _? #:nodoc:
|
41
|
-
@chain << [[ :_? ],nil]
|
42
|
-
self
|
43
|
-
end
|
44
|
-
end
|
45
|
-
|
46
|
-
class Object
|
47
|
-
#
|
48
|
-
# Begins a FragileMethodChain.
|
49
|
-
#
|
50
|
-
def _?
|
51
|
-
FragileMethodChain.new(self)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
=begin
|
56
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
57
|
-
|
58
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
59
|
-
purpose with or without fee is hereby granted, provided that the above
|
60
|
-
copyright notice and this permission notice appear in all copies.
|
61
|
-
|
62
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
63
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
64
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
65
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
66
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
67
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
68
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
69
|
-
=end
|
70
|
-
|
1
|
+
|
2
|
+
#
|
3
|
+
# Invokes a method chain until one method returns a falsy value.
|
4
|
+
#
|
5
|
+
# For example:
|
6
|
+
#
|
7
|
+
# a._?.b.c.d._!
|
8
|
+
# nested_hash._?[:a][:b][:c]._!
|
9
|
+
#
|
10
|
+
class FragileMethodChain
|
11
|
+
#
|
12
|
+
# Creates a FragileMethodChain which will send its first method to +o+
|
13
|
+
#
|
14
|
+
def initialize o
|
15
|
+
@o = o
|
16
|
+
@chain = []
|
17
|
+
end
|
18
|
+
|
19
|
+
#
|
20
|
+
# Finalises the FragileMethodChain.
|
21
|
+
#
|
22
|
+
# The final result will be the first +nil+ or +false+ value
|
23
|
+
# returned in the chain, or its end result.
|
24
|
+
#
|
25
|
+
def _!
|
26
|
+
@chain.inject(@o) do |o,x|
|
27
|
+
a,b = x
|
28
|
+
break o unless o
|
29
|
+
o.__send__(*a, &b)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
# Record the method args/block
|
34
|
+
def method_missing *a, &b #:nodoc:
|
35
|
+
@chain << [a,b]
|
36
|
+
self
|
37
|
+
end
|
38
|
+
|
39
|
+
# Explicitly record :_? as a method in the chain.
|
40
|
+
def _? #:nodoc:
|
41
|
+
@chain << [[ :_? ],nil]
|
42
|
+
self
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class Object
|
47
|
+
#
|
48
|
+
# Begins a FragileMethodChain.
|
49
|
+
#
|
50
|
+
def _?
|
51
|
+
FragileMethodChain.new(self)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
=begin
|
56
|
+
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
57
|
+
|
58
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
59
|
+
purpose with or without fee is hereby granted, provided that the above
|
60
|
+
copyright notice and this permission notice appear in all copies.
|
61
|
+
|
62
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
63
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
64
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
65
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
66
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
67
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
68
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
69
|
+
=end
|
70
|
+
|
data/lib/mug/iterator.rb
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
require_relative 'iterator/for'
|
2
|
-
require_relative 'iterator/method'
|
1
|
+
require_relative 'iterator/for'
|
2
|
+
require_relative 'iterator/method'
|
data/lib/mug/iterator/for.rb
CHANGED
@@ -1,29 +1,29 @@
|
|
1
|
-
|
2
|
-
require_relative '../iterator_c'
|
3
|
-
|
4
|
-
class Object
|
5
|
-
#
|
6
|
-
# Creates a new Iterator for the method named +meth+
|
7
|
-
#
|
8
|
-
def iter_for meth, *args
|
9
|
-
Iterator.new self, meth, *args
|
10
|
-
end
|
11
|
-
alias :to_iter :iter_for
|
12
|
-
end
|
13
|
-
|
14
|
-
=begin
|
15
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
16
|
-
|
17
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
18
|
-
purpose with or without fee is hereby granted, provided that the above
|
19
|
-
copyright notice and this permission notice appear in all copies.
|
20
|
-
|
21
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
22
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
23
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
24
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
25
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
26
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
27
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
28
|
-
=end
|
29
|
-
|
1
|
+
|
2
|
+
require_relative '../iterator_c'
|
3
|
+
|
4
|
+
class Object
|
5
|
+
#
|
6
|
+
# Creates a new Iterator for the method named +meth+
|
7
|
+
#
|
8
|
+
def iter_for meth, *args
|
9
|
+
Iterator.new self, meth, *args
|
10
|
+
end
|
11
|
+
alias :to_iter :iter_for
|
12
|
+
end
|
13
|
+
|
14
|
+
=begin
|
15
|
+
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
16
|
+
|
17
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
18
|
+
purpose with or without fee is hereby granted, provided that the above
|
19
|
+
copyright notice and this permission notice appear in all copies.
|
20
|
+
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
22
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
23
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
24
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
25
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
26
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
27
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
28
|
+
=end
|
29
|
+
|
data/lib/mug/iterator/method.rb
CHANGED
@@ -1,29 +1,29 @@
|
|
1
|
-
|
2
|
-
require_relative '../iterator_c'
|
3
|
-
|
4
|
-
class Method
|
5
|
-
#
|
6
|
-
# Creates a new Iterator for this method, initially invoked
|
7
|
-
# on this method's receiver.
|
8
|
-
#
|
9
|
-
def to_iter *args
|
10
|
-
Iterator.new receiver, name, *args
|
11
|
-
end
|
12
|
-
end
|
13
|
-
|
14
|
-
=begin
|
15
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
16
|
-
|
17
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
18
|
-
purpose with or without fee is hereby granted, provided that the above
|
19
|
-
copyright notice and this permission notice appear in all copies.
|
20
|
-
|
21
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
22
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
23
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
24
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
25
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
26
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
27
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
28
|
-
=end
|
29
|
-
|
1
|
+
|
2
|
+
require_relative '../iterator_c'
|
3
|
+
|
4
|
+
class Method
|
5
|
+
#
|
6
|
+
# Creates a new Iterator for this method, initially invoked
|
7
|
+
# on this method's receiver.
|
8
|
+
#
|
9
|
+
def to_iter *args
|
10
|
+
Iterator.new receiver, name, *args
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
=begin
|
15
|
+
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
16
|
+
|
17
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
18
|
+
purpose with or without fee is hereby granted, provided that the above
|
19
|
+
copyright notice and this permission notice appear in all copies.
|
20
|
+
|
21
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
22
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
23
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
24
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
25
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
26
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
27
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
28
|
+
=end
|
29
|
+
|
data/lib/mug/iterator_c.rb
CHANGED
@@ -1,44 +1,44 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
# A special class of Enumerator that repeatedly invokes a method.
|
4
|
-
#
|
5
|
-
# Initially the method is send to the given +obj+, but subsequent
|
6
|
-
# invocations are sent to the result of the previous invocation.
|
7
|
-
#
|
8
|
-
# Example:
|
9
|
-
#
|
10
|
-
# 0.iter_for(:next).take(5) #=> [0,1,2,3,4]
|
11
|
-
#
|
12
|
-
class Iterator < Enumerator
|
13
|
-
#
|
14
|
-
# Creates a new Iterator for method +meth+, to be
|
15
|
-
# called initially on object +obj+.
|
16
|
-
#
|
17
|
-
# All method calls will have +args+ as parameters.
|
18
|
-
#
|
19
|
-
def initialize obj, meth, *args
|
20
|
-
super() do |y|
|
21
|
-
loop do
|
22
|
-
y << obj
|
23
|
-
obj = obj.send(meth, *args)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
=begin
|
30
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
31
|
-
|
32
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
33
|
-
purpose with or without fee is hereby granted, provided that the above
|
34
|
-
copyright notice and this permission notice appear in all copies.
|
35
|
-
|
36
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
37
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
38
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
39
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
40
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
41
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
42
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
43
|
-
=end
|
44
|
-
|
1
|
+
|
2
|
+
#
|
3
|
+
# A special class of Enumerator that repeatedly invokes a method.
|
4
|
+
#
|
5
|
+
# Initially the method is send to the given +obj+, but subsequent
|
6
|
+
# invocations are sent to the result of the previous invocation.
|
7
|
+
#
|
8
|
+
# Example:
|
9
|
+
#
|
10
|
+
# 0.iter_for(:next).take(5) #=> [0,1,2,3,4]
|
11
|
+
#
|
12
|
+
class Iterator < Enumerator
|
13
|
+
#
|
14
|
+
# Creates a new Iterator for method +meth+, to be
|
15
|
+
# called initially on object +obj+.
|
16
|
+
#
|
17
|
+
# All method calls will have +args+ as parameters.
|
18
|
+
#
|
19
|
+
def initialize obj, meth, *args
|
20
|
+
super() do |y|
|
21
|
+
loop do
|
22
|
+
y << obj
|
23
|
+
obj = obj.send(meth, *args)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
=begin
|
30
|
+
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
31
|
+
|
32
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
33
|
+
purpose with or without fee is hereby granted, provided that the above
|
34
|
+
copyright notice and this permission notice appear in all copies.
|
35
|
+
|
36
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
37
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
38
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
39
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
40
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
41
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
42
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
43
|
+
=end
|
44
|
+
|
data/lib/mug/maybe.rb
CHANGED
@@ -1,59 +1,59 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
# Invokes methods on a wrapped object, if that object is truthy.
|
4
|
-
#
|
5
|
-
class MaybeDelegator
|
6
|
-
#
|
7
|
-
# Creates a new MaybeDelegator, wrapping +o+
|
8
|
-
#
|
9
|
-
def initialize o
|
10
|
-
@o = o
|
11
|
-
end
|
12
|
-
|
13
|
-
#
|
14
|
-
# Returns this MaybeDelegator object.
|
15
|
-
#
|
16
|
-
def maybe
|
17
|
-
self
|
18
|
-
end
|
19
|
-
|
20
|
-
# Calls the method on +@o+ if it's truthy.
|
21
|
-
def method_missing *a, &b #:nodoc:
|
22
|
-
@o && @o.send(*a, &b)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
class Object
|
27
|
-
#
|
28
|
-
# Do something if this object is truthy.
|
29
|
-
#
|
30
|
-
# If a block is given, it is executed in the context of this
|
31
|
-
# object, iff this object is neither +nil+ nor +false+.
|
32
|
-
#
|
33
|
-
# If no block is given, returns a MaybeDelegator object.
|
34
|
-
#
|
35
|
-
def maybe &b
|
36
|
-
if b
|
37
|
-
self && instance_eval(&b)
|
38
|
-
else
|
39
|
-
MaybeDelegator.new(self)
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
=begin
|
45
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
46
|
-
|
47
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
48
|
-
purpose with or without fee is hereby granted, provided that the above
|
49
|
-
copyright notice and this permission notice appear in all copies.
|
50
|
-
|
51
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
52
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
53
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
54
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
55
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
56
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
57
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
58
|
-
=end
|
59
|
-
|
1
|
+
|
2
|
+
#
|
3
|
+
# Invokes methods on a wrapped object, if that object is truthy.
|
4
|
+
#
|
5
|
+
class MaybeDelegator
|
6
|
+
#
|
7
|
+
# Creates a new MaybeDelegator, wrapping +o+
|
8
|
+
#
|
9
|
+
def initialize o
|
10
|
+
@o = o
|
11
|
+
end
|
12
|
+
|
13
|
+
#
|
14
|
+
# Returns this MaybeDelegator object.
|
15
|
+
#
|
16
|
+
def maybe
|
17
|
+
self
|
18
|
+
end
|
19
|
+
|
20
|
+
# Calls the method on +@o+ if it's truthy.
|
21
|
+
def method_missing *a, &b #:nodoc:
|
22
|
+
@o && @o.send(*a, &b)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class Object
|
27
|
+
#
|
28
|
+
# Do something if this object is truthy.
|
29
|
+
#
|
30
|
+
# If a block is given, it is executed in the context of this
|
31
|
+
# object, iff this object is neither +nil+ nor +false+.
|
32
|
+
#
|
33
|
+
# If no block is given, returns a MaybeDelegator object.
|
34
|
+
#
|
35
|
+
def maybe &b
|
36
|
+
if b
|
37
|
+
self && instance_eval(&b)
|
38
|
+
else
|
39
|
+
MaybeDelegator.new(self)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
=begin
|
45
|
+
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
46
|
+
|
47
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
48
|
+
purpose with or without fee is hereby granted, provided that the above
|
49
|
+
copyright notice and this permission notice appear in all copies.
|
50
|
+
|
51
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
52
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
53
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
54
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
55
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
56
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
57
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
58
|
+
=end
|
59
|
+
|