mug 0.0.1
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 +7 -0
- data/lib/mug.rb +8 -0
- data/lib/mug/bool.rb +112 -0
- data/lib/mug/fragile-method-chain.rb +53 -0
- data/lib/mug/iterator.rb +27 -0
- data/lib/mug/iterator/for.rb +12 -0
- data/lib/mug/iterator/method.rb +12 -0
- data/lib/mug/maybe.rb +42 -0
- data/lib/mug/self.rb +16 -0
- metadata +55 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7892b05f508f2c796669e30d404d1b637976479e
|
4
|
+
data.tar.gz: 8b88ce7c7d3ebbc082045b96a3058ce74d648c63
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: a59cd47d21e27a63fb2b3b6315e5c0fd64fbc4add1372763c3ecdfb95349f05c6550c7d75fd2233f3a8a0159395014ce829625dfc3b5cde34b3341cd3a934bf3
|
7
|
+
data.tar.gz: 683a341d4bdd574f225abf5911a7dc18a5011ccc5e6d41ea40595b7418c86fe9f16961da9c03a201d916bdcc443ad4eec52a29fc73efe3a55317aca18400595e
|
data/lib/mug.rb
ADDED
data/lib/mug/bool.rb
ADDED
@@ -0,0 +1,112 @@
|
|
1
|
+
|
2
|
+
#
|
3
|
+
# Converts arg to a boolean (true or false).
|
4
|
+
#
|
5
|
+
def Bool(arg)
|
6
|
+
!!arg
|
7
|
+
end
|
8
|
+
|
9
|
+
class Object
|
10
|
+
#
|
11
|
+
# Converts obj to a boolean.
|
12
|
+
#
|
13
|
+
def to_bool
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Converts obj to a boolean.
|
19
|
+
#
|
20
|
+
def to_b
|
21
|
+
true
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def nil.to_bool; false; end
|
26
|
+
def nil.to_b; false; end
|
27
|
+
def false.to_bool; false; end
|
28
|
+
def false.to_b; false; end
|
29
|
+
|
30
|
+
class Numeric
|
31
|
+
#
|
32
|
+
# Converts num to a boolean.
|
33
|
+
# Returns true if not zero.
|
34
|
+
#
|
35
|
+
def to_b
|
36
|
+
self != 0
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class Float
|
41
|
+
#
|
42
|
+
# Converts num to a boolean.
|
43
|
+
# Returns true if not zero or NaN.
|
44
|
+
# Note: -0.0 is false, and +/-infinity are true.
|
45
|
+
#
|
46
|
+
def to_b
|
47
|
+
!(self.zero? || self.nan?)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class String
|
52
|
+
#
|
53
|
+
# Converts str to a boolean.
|
54
|
+
# Returns true if not empty.
|
55
|
+
#
|
56
|
+
def to_b
|
57
|
+
!empty?
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
class Array
|
62
|
+
#
|
63
|
+
# Converts ary to a boolean.
|
64
|
+
# Returns true if not empty.
|
65
|
+
#
|
66
|
+
def to_b
|
67
|
+
!empty?
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
class Hash
|
72
|
+
#
|
73
|
+
# Converts hsh to a boolean.
|
74
|
+
# Returns true if not empty.
|
75
|
+
#
|
76
|
+
def to_b
|
77
|
+
!empty?
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
module Enumerable
|
82
|
+
#
|
83
|
+
# Converts enum to a boolean.
|
84
|
+
# Returns true if there are any elements.
|
85
|
+
#
|
86
|
+
def to_b
|
87
|
+
any?{ true }
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
if RUBY_VERSION.to_i >= 2
|
92
|
+
class Enumerator
|
93
|
+
#
|
94
|
+
# Converts enum to a boolean.
|
95
|
+
# Returns true if there are any elements.
|
96
|
+
#
|
97
|
+
def to_b
|
98
|
+
size.to_b
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
class Exception
|
104
|
+
#
|
105
|
+
# Converts ex to a boolean.
|
106
|
+
# All Exceptions are considered false.
|
107
|
+
#
|
108
|
+
def to_b
|
109
|
+
false
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
@@ -0,0 +1,53 @@
|
|
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
|
data/lib/mug/iterator.rb
ADDED
@@ -0,0 +1,27 @@
|
|
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
|
data/lib/mug/maybe.rb
ADDED
@@ -0,0 +1,42 @@
|
|
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
|
data/lib/mug/self.rb
ADDED
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mug
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Matthew Kerwin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-07-24 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: |
|
14
|
+
== MUG: Matty's Ultimate Gem
|
15
|
+
|
16
|
+
A collection of wonders to astound the mind!!
|
17
|
+
email:
|
18
|
+
- matthew@kerwin.net.au
|
19
|
+
executables: []
|
20
|
+
extensions: []
|
21
|
+
extra_rdoc_files: []
|
22
|
+
files:
|
23
|
+
- lib/mug.rb
|
24
|
+
- lib/mug/fragile-method-chain.rb
|
25
|
+
- lib/mug/self.rb
|
26
|
+
- lib/mug/iterator.rb
|
27
|
+
- lib/mug/iterator/for.rb
|
28
|
+
- lib/mug/iterator/method.rb
|
29
|
+
- lib/mug/maybe.rb
|
30
|
+
- lib/mug/bool.rb
|
31
|
+
homepage: http://phluid61.github.com/mug
|
32
|
+
licenses:
|
33
|
+
- ISC License
|
34
|
+
metadata: {}
|
35
|
+
post_install_message:
|
36
|
+
rdoc_options: []
|
37
|
+
require_paths:
|
38
|
+
- lib
|
39
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - '>='
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: '0'
|
49
|
+
requirements: []
|
50
|
+
rubyforge_project:
|
51
|
+
rubygems_version: 2.0.2
|
52
|
+
signing_key:
|
53
|
+
specification_version: 4
|
54
|
+
summary: 'MUG: Matty''s Ultimate Gem'
|
55
|
+
test_files: []
|