custom_boolean 0.1.1 → 0.1.2
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.
- data/CHANGELOG +8 -1
- data/Rakefile +9 -1
- data/lib/custom_boolean.rb +132 -2
- data/lib/custom_boolean/version.rb +1 -1
- metadata +8 -7
data/CHANGELOG
CHANGED
data/Rakefile
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'rake/gempackagetask'
|
2
|
+
require 'rake/rdoctask'
|
2
3
|
require './lib/custom_boolean/version'
|
3
4
|
|
4
5
|
$dlext = Config::CONFIG['DLEXT']
|
@@ -14,7 +15,10 @@ specification = Gem::Specification.new do |s|
|
|
14
15
|
s.require_path = 'lib'
|
15
16
|
s.platform = Gem::Platform::RUBY
|
16
17
|
s.homepage = "http://banisterfiend.wordpress.com"
|
17
|
-
s.has_rdoc =
|
18
|
+
s.has_rdoc = 'yard'
|
19
|
+
s.extra_rdoc_files = ["README.markdown"]
|
20
|
+
s.rdoc_options << '--main' << 'README.markdown'
|
21
|
+
|
18
22
|
s.files = ["Rakefile", "README.markdown", "CHANGELOG",
|
19
23
|
"lib/custom_boolean.rb", "lib/custom_boolean/version.rb"] +
|
20
24
|
FileList["examples/*.rb", "test/*.rb"].to_a
|
@@ -25,3 +29,7 @@ Rake::GemPackageTask.new(specification) do |package|
|
|
25
29
|
package.need_tar = false
|
26
30
|
end
|
27
31
|
|
32
|
+
Rake::RDocTask.new do |rd|
|
33
|
+
rd.main = "README.markdown"
|
34
|
+
rd.rdoc_files.include("README.markdown", "lib/**/*.rb")
|
35
|
+
end
|
data/lib/custom_boolean.rb
CHANGED
@@ -1,17 +1,40 @@
|
|
1
1
|
# CustomBoolean by John Mair (banisterfiend)
|
2
2
|
# MIT license
|
3
3
|
|
4
|
-
|
5
4
|
direc = File.dirname(__FILE__)
|
6
5
|
require "#{direc}/custom_boolean/version"
|
7
6
|
|
7
|
+
# @author John Mair (banisterfiend)
|
8
8
|
class CustomBoolean
|
9
9
|
module Operators
|
10
|
+
|
11
|
+
# Equivalent of **&&** for CustomBoolean truthiness.
|
12
|
+
# Differs from regular **&&** as it uses CustomBoolean truthiness
|
13
|
+
#
|
14
|
+
# **NOTE:** It is usually better to use the {#and} alias, as fewer objects override this
|
15
|
+
# method)
|
16
|
+
#
|
17
|
+
# @param The rhs of the boolean *and* operator
|
18
|
+
# @return [Boolean]
|
19
|
+
# @example
|
20
|
+
# obj1 & obj2
|
21
|
+
# obj1.and obj2
|
10
22
|
def &(other)
|
11
23
|
CustomBoolean.truthy?(self) && CustomBoolean.truthy?(other)
|
12
24
|
end
|
13
25
|
alias_method :and, :"&"
|
14
26
|
|
27
|
+
# Equivalent of **||** for CustomBoolean truthiness.
|
28
|
+
# Differs from regular **||** as it uses CustomBoolean truthiness
|
29
|
+
#
|
30
|
+
# **NOTE:** It is usually better to use the {#or} alias, as fewer objects override this
|
31
|
+
# method)
|
32
|
+
#
|
33
|
+
# @param The rhs of the boolean *or* operator
|
34
|
+
# @return [Boolean]
|
35
|
+
# @example
|
36
|
+
# obj1 | obj2
|
37
|
+
# obj1.or obj2
|
15
38
|
def |(other)
|
16
39
|
CustomBoolean.truthy?(self) || CustomBoolean.truthy?(other)
|
17
40
|
end
|
@@ -19,14 +42,38 @@ class CustomBoolean
|
|
19
42
|
end
|
20
43
|
end
|
21
44
|
|
45
|
+
# Unfortunately patching these objects is necessary :(
|
22
46
|
true.extend(CustomBoolean::Operators)
|
23
47
|
false.extend(CustomBoolean::Operators)
|
24
48
|
Object.send(:include, CustomBoolean::Operators)
|
25
49
|
|
50
|
+
# Equivalent of **!** for CustomBoolean truthiness.
|
51
|
+
# Differs from regular **!** as it uses CustomBoolean truthiness
|
52
|
+
#
|
53
|
+
# @param The expression to be negated
|
54
|
+
# @return [Boolean]
|
55
|
+
# @example
|
56
|
+
# negate(obj)
|
26
57
|
def negate(expr)
|
27
58
|
!CustomBoolean.truthy?(expr)
|
28
59
|
end
|
29
60
|
|
61
|
+
# Equivalent of **if** for CustomBoolean truthiness.
|
62
|
+
# Differs from regular **if** as it uses CustomBoolean truthiness
|
63
|
+
#
|
64
|
+
# @example basic usage
|
65
|
+
# Use as follows: if_(condition) { ... }
|
66
|
+
# Other conditionals chain on to the end of if_() as follows:
|
67
|
+
#
|
68
|
+
# if_(condition) { ... }.else { ... }
|
69
|
+
# @example if-expression form
|
70
|
+
# Can turn if_() statement into if-expression by prefixing with a `+`
|
71
|
+
#
|
72
|
+
# value = +if_(true) { :hello }
|
73
|
+
# value #=> :hello
|
74
|
+
# @param an expression to evaluate
|
75
|
+
# @return [CustomBoolean]
|
76
|
+
# @yield the block will be executed if the condition evalues to true
|
30
77
|
def if_(condition, &block)
|
31
78
|
truth = !!CustomBoolean.truthy?(condition)
|
32
79
|
bvalue = block.call if truth
|
@@ -39,11 +86,42 @@ alias _if if_
|
|
39
86
|
alias if? if_
|
40
87
|
|
41
88
|
class CustomBoolean
|
42
|
-
|
89
|
+
|
90
|
+
# @return [Boolean]
|
91
|
+
attr_accessor :truth_value
|
92
|
+
|
93
|
+
# @return [Object] The value of the block that was executed in the
|
94
|
+
# if/else_if/else
|
95
|
+
attr_accessor :value
|
43
96
|
|
44
97
|
class << self
|
98
|
+
|
99
|
+
# @return [Proc] The proc that defines the truth test
|
45
100
|
attr_accessor :truth_test
|
46
101
|
|
102
|
+
# Tests whether *condition* is truthy according to
|
103
|
+
# CustomBoolean truthiness.
|
104
|
+
# CustomBoolean truthiness is determined by the proc referenced
|
105
|
+
# by CustomBoolean.truth_test.
|
106
|
+
#
|
107
|
+
# Built in Truth tests include:
|
108
|
+
# CustomBoolean::RUBY_TRUTH
|
109
|
+
#
|
110
|
+
# CustomBoolean::PYTHON_TRUTH
|
111
|
+
#
|
112
|
+
# CustomBoolean::PERL_TRUTH
|
113
|
+
#
|
114
|
+
# CustomBoolean::C_TRUTH
|
115
|
+
#
|
116
|
+
# CustomBoolean::STRICT_TRUTH
|
117
|
+
#
|
118
|
+
# @example changing truthiness to a preset
|
119
|
+
# CustomBoolean.truth_test = CustomBoolean::PYTHON_TRUTH
|
120
|
+
# @example user-defined truthiness
|
121
|
+
# # only :horse is true:
|
122
|
+
# CustomBoolean.truth_test = proc { |expr| expr == :horse }
|
123
|
+
# @param an expression to evaluate
|
124
|
+
# @return [Boolean]
|
47
125
|
def truthy?(condition)
|
48
126
|
self.truth_test.call(condition)
|
49
127
|
end
|
@@ -67,10 +145,42 @@ class CustomBoolean
|
|
67
145
|
self.value = block_value
|
68
146
|
end
|
69
147
|
|
148
|
+
# Prefixing +if_+ with `+` turns if-statement into if-expression
|
149
|
+
# by invoking #value on CustomBoolean object.
|
150
|
+
# @return [Object] extracts the value of the if, transforming it
|
151
|
+
# into an if-expression
|
152
|
+
# @example single if-expression example
|
153
|
+
# +if(true) { :hello } #=> :hello
|
154
|
+
# +if(fale) { :hello } #=> nil
|
155
|
+
# @example if-else-expression example
|
156
|
+
# +if(false) {
|
157
|
+
# :hello
|
158
|
+
# }.
|
159
|
+
# else {
|
160
|
+
# :goodbye
|
161
|
+
# }
|
162
|
+
# #=> :goodbye
|
70
163
|
def +@
|
71
164
|
self.value
|
72
165
|
end
|
73
166
|
|
167
|
+
# Equivalent of **elsif** for CustomBoolean truthiness.
|
168
|
+
# Must be chained after an if_() or another else_if()
|
169
|
+
#
|
170
|
+
# Differs from regular **elsif** as it uses CustomBoolean truthiness
|
171
|
+
#
|
172
|
+
# @example else_if example
|
173
|
+
# if_(cond) {
|
174
|
+
# :hello
|
175
|
+
# }.
|
176
|
+
# else_if(cond2) {
|
177
|
+
# :goodbye
|
178
|
+
# }
|
179
|
+
# @param [Object] an expression to evaluate
|
180
|
+
# @return [CustomBoolean]
|
181
|
+
# @yield the block will be executed if the condition evalues to true
|
182
|
+
# (so long as no prior *else_if* or *if* has evaluated to true further
|
183
|
+
# up the chain)
|
74
184
|
def else_if(condition, &block)
|
75
185
|
raise InvalidConditional, "No further conditionals allowed after an else." if self.truth_value == :else_reached
|
76
186
|
|
@@ -90,6 +200,26 @@ class CustomBoolean
|
|
90
200
|
alias elsif? else_if
|
91
201
|
alias elsif! else_if
|
92
202
|
|
203
|
+
# Equivalent of **else** for CustomBoolean truthiness.
|
204
|
+
# Must be chained after an if_() or an else_if()
|
205
|
+
#
|
206
|
+
# Differs from regular **else** as it uses CustomBoolean truthiness
|
207
|
+
#
|
208
|
+
# No other conditionals may be chained after an +else+.
|
209
|
+
# In event a conditional is chained after an +else+ an
|
210
|
+
# **InvalidConditional** exception will be raised.
|
211
|
+
#
|
212
|
+
# @example else example
|
213
|
+
# if_(cond) {
|
214
|
+
# :hello
|
215
|
+
# }.
|
216
|
+
# else {
|
217
|
+
# :goodbye
|
218
|
+
# }
|
219
|
+
# @return [CustomBoolean]
|
220
|
+
# @yield the block will be executed if the condition evalues to true
|
221
|
+
# (so long as no prior *else_if* or *if* has evaluated to true further
|
222
|
+
# up the chain)
|
93
223
|
def else(&block)
|
94
224
|
raise InvalidConditional, "No further conditionals allowed after an else." if self.truth_value == :else_reached
|
95
225
|
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 1
|
8
|
-
-
|
9
|
-
version: 0.1.
|
8
|
+
- 2
|
9
|
+
version: 0.1.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- John Mair (banisterfiend)
|
@@ -24,8 +24,8 @@ executables: []
|
|
24
24
|
|
25
25
|
extensions: []
|
26
26
|
|
27
|
-
extra_rdoc_files:
|
28
|
-
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.markdown
|
29
29
|
files:
|
30
30
|
- Rakefile
|
31
31
|
- README.markdown
|
@@ -37,13 +37,14 @@ files:
|
|
37
37
|
- examples/example_nested.rb
|
38
38
|
- examples/example_operators.rb
|
39
39
|
- test/test.rb
|
40
|
-
has_rdoc:
|
40
|
+
has_rdoc: yard
|
41
41
|
homepage: http://banisterfiend.wordpress.com
|
42
42
|
licenses: []
|
43
43
|
|
44
44
|
post_install_message:
|
45
|
-
rdoc_options:
|
46
|
-
|
45
|
+
rdoc_options:
|
46
|
+
- --main
|
47
|
+
- README.markdown
|
47
48
|
require_paths:
|
48
49
|
- lib
|
49
50
|
required_ruby_version: !ruby/object:Gem::Requirement
|