Class: ATypes::BooleanWrap

Inherits:
Wrapper show all
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

Instance Method Summary collapse

Methods inherited from Wrapper

#content

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.

Examples:

BooleanWrap.new(true) & BooleanWrap.new(true) # => true
BooleanWrap.new('y')  & "hello"           # => true
BooleanWrap.new('y')  & nil               # => false


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.

Examples:

BooleanWrap.new(true)   ^ BooleanWrap.new(true) # => false
BooleanWrap.new('y')    ^ "hello"           # => true
BooleanWrap.new('y')    ^ nil               # => true
BooleanWrap.new(false)  ^ nil              # => false


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



56
57
58
# File 'lib/a_types/decorators/boolean_wrap.rb', line 56

def false?
  !to_bool
end

#falsey?true, false



72
73
74
# File 'lib/a_types/decorators/boolean_wrap.rb', line 72

def falsey?
  !content
end

#to_booltrue, ... 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.

See Also:



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_sString Also known as: inspect

Returns string representation of this BooleanWrap’s #to_bool interpretation.

Examples:

BooleanWrap.new('y').to_s   # => "true"


141
142
143
# File 'lib/a_types/decorators/boolean_wrap.rb', line 141

def to_s
  to_bool.to_s
end

#true?true, false



49
50
51
# File 'lib/a_types/decorators/boolean_wrap.rb', line 49

def true?
  to_bool
end

#truthy?true, false



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.

Examples:

BooleanWrap.new(true)   | BooleanWrap.new(true) # => true
BooleanWrap.new('y')    | "hello"           # => true
BooleanWrap.new('y')    | nil               # => true
BooleanWrap.new(false)  | nil              # => false


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