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.

Parameters:

  • source (Object)

    to be converted to a boolean value.

Returns:

  • (true, false, nil)

    depending on whether and how the object is convertible



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

Parameters:

  • other (BooleanWrap, true, false, Object)

    to logically combine with this BooleanWrap.

Returns:

  • (true, false)

    result of logical AND.



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

Parameters:

  • other (BooleanWrap, true, false, Object)

    to logically combine with this BooleanWrap.

Returns:

  • (true, false)

    result of logical OR.



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.

Returns:

  • (true, false)

    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.

Returns:

  • (true, false)

    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_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.

Returns:

  • (true, false, nil)

    depending on whether the object may be converted and which value it contains.



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.

Returns:

  • (true, false, nil)

    depending on whether the object may be converted and which value it contains.

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"

Returns:

  • (String)

    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.

Returns:

  • (true, false)

    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.

Returns:

  • (true, false)

    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.

Examples:

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

Parameters:

  • other (BooleanWrap, true, false, Object)

    to logically combine with this BooleanWrap.

Returns:

  • (true, false)

    result of logical OR.



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