Class: ATypes::BooleanWrap
- Defined in:
- lib/a_types/decorators/boolean_wrap.rb
Overview
A decorator to any object providing Boolean behavior. In comparisons, it always returns the real true or false instance to avoid ambiguity. By default, it will handle special values for different object classes, like certain strings, e.g. transforming the *‘true’* to an instance of the TrueClass. This behavior may be turned off, though, in which case the standard Ruby evaluation of truthy and falsey will be respected.
Class Method Summary collapse
-
.try_convert(source) ⇒ true, ...
Will try to convert given object to a boolean value according to #to_bool!.
Instance Method Summary collapse
-
#&(other) ⇒ true, false
Will operate a logical AND with other, based on this BooleanWrap’s #to_bool interpretation.
-
#^(other) ⇒ true, false
Will operate a logical XOR with other, based on this BooleanWrap’s #to_bool interpretation.
-
#false? ⇒ true, false
Depending on whether the content interprets to false.
-
#falsey? ⇒ true, false
Whether the content is falsey according to Ruby’s rules.
-
#to_bool ⇒ true, ...
(also: #truth, #to_b)
Extracts the truth from this BooleanWrap’s value according to the following rules: - for any kind of numeric: values above 0 are true, others false.
-
#to_bool! ⇒ true, ...
Will try to convert this BooleanWrap according to the rules of #to_bool, however, it will raise an ArgumentError if conversion fails.
-
#to_s ⇒ String
(also: #inspect)
String representation of this BooleanWrap’s #to_bool interpretation.
-
#true? ⇒ true, false
Depending on whether the content interprets to true.
-
#truthy? ⇒ true, false
Whether the content is truthy according to Ruby’s rules.
-
#|(other) ⇒ true, false
Will operate a logical OR with other, based on this BooleanWrap’s #to_bool interpretation.
Methods inherited from Wrapper
Class Method Details
.try_convert(source) ⇒ true, ...
Will try to convert given object to a boolean value according to #to_bool!. If this fails, it will return nil.
153 154 155 |
# File 'lib/a_types/decorators/boolean_wrap.rb', line 153 def self.try_convert(source) new(source).to_bool! end |
Instance Method Details
#&(other) ⇒ true, false
Will operate a logical AND with other, based on this BooleanWrap’s #to_bool interpretation. It will therefore handle Ruby’s native booleans and the BooleanWrap instances in the very same way. Otherwise, the logic abides Ruby’s default truthy interpretation.
90 91 92 93 |
# File 'lib/a_types/decorators/boolean_wrap.rb', line 90 def &(other) other_value = other.respond_to?(:truthy?) ? other.truthy? : other to_bool & other_value end |
#^(other) ⇒ true, false
Will operate a logical XOR with other, based on this BooleanWrap’s #to_bool interpretation. It will therefore handle Ruby’s native booleans and the BooleanWrap instances in the very same way. Otherwise, the logic abides Ruby’sdefault truthy interpretation.
130 131 132 133 |
# File 'lib/a_types/decorators/boolean_wrap.rb', line 130 def ^(other) other_value = other.respond_to?(:truthy?) ? other.truthy? : other to_bool ^ other_value end |
#false? ⇒ true, false
Returns depending on whether the content interprets to false.
56 57 58 |
# File 'lib/a_types/decorators/boolean_wrap.rb', line 56 def false? !to_bool end |
#falsey? ⇒ true, false
Returns whether the content is falsey according to Ruby’s rules.
72 73 74 |
# File 'lib/a_types/decorators/boolean_wrap.rb', line 72 def falsey? !content end |
#to_bool ⇒ true, ... Also known as: truth, to_b
Extracts the truth from this BooleanWrap’s value according to the following rules:
-
for any kind of numeric: values above 0 are true, others false.
-
for strings: any string equal to either of the following
["y", "Y", "1", "yes", "Yes", "YES", "true", "True", "TRUE"]
will be true, any string equal to either of these
["n", "N", "-1", "no", "No", "NO", "false", "False", "FALSE"]
will be false.
-
any other object will be transformed to a hard boolean according Ruby’s default truthy evaluation.
26 27 28 |
# File 'lib/a_types/decorators/boolean_wrap.rb', line 26 def to_bool @truth ||= extract_truth(content) end |
#to_bool! ⇒ true, ...
Will try to convert this BooleanWrap according to the rules of #to_bool, however, it will raise an ArgumentError if conversion fails.
39 40 41 42 43 44 45 |
# File 'lib/a_types/decorators/boolean_wrap.rb', line 39 def to_bool! error_msg = ">#{content}< can't be interpreted as boolean!" converted = to_bool fail(ArgumentError, error_msg) if converted.nil? converted end |
#to_s ⇒ String Also known as: inspect
Returns string representation of this BooleanWrap’s #to_bool interpretation.
141 142 143 |
# File 'lib/a_types/decorators/boolean_wrap.rb', line 141 def to_s to_bool.to_s end |
#true? ⇒ true, false
Returns depending on whether the content interprets to true.
49 50 51 |
# File 'lib/a_types/decorators/boolean_wrap.rb', line 49 def true? to_bool end |
#truthy? ⇒ true, false
Returns whether the content is truthy according to Ruby’s rules.
63 64 65 66 67 |
# File 'lib/a_types/decorators/boolean_wrap.rb', line 63 def truthy? # content can literally be ANYTHING, thus double negation should be the # tool of choice here. !!content end |
#|(other) ⇒ true, false
Will operate a logical OR with other, based on this BooleanWrap’s #to_bool interpretation. It will therefore handle Ruby’s native booleans and the BooleanWrap instances in the very same way. Otherwise, the logic abides Ruby’s default truthy interpretation.
110 111 112 113 |
# File 'lib/a_types/decorators/boolean_wrap.rb', line 110 def |(other) other_value = other.respond_to?(:truthy?) ? other.truthy? : other to_bool | other_value end |