Class: TTY::Conversion::BooleanConverter
- Inherits:
-
Object
- Object
- TTY::Conversion::BooleanConverter
- Defined in:
- lib/tty/conversion/converter/boolean.rb
Instance Attribute Summary collapse
-
#source ⇒ Object
readonly
Returns the value of attribute source.
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #boolean?(value) ⇒ Boolean
-
#convert(value) ⇒ Object
Convert value to boolean type including range of strings such as.
-
#initialize(source = String) ⇒ BooleanConverter
constructor
A new instance of BooleanConverter.
Constructor Details
#initialize(source = String) ⇒ BooleanConverter
Returns a new instance of BooleanConverter.
11 12 13 14 |
# File 'lib/tty/conversion/converter/boolean.rb', line 11 def initialize(source = String) @source = source @target = [TrueClass, FalseClass] end |
Instance Attribute Details
#source ⇒ Object (readonly)
Returns the value of attribute source.
9 10 11 |
# File 'lib/tty/conversion/converter/boolean.rb', line 9 def source @source end |
#target ⇒ Object (readonly)
Returns the value of attribute target.
7 8 9 |
# File 'lib/tty/conversion/converter/boolean.rb', line 7 def target @target end |
Instance Method Details
#boolean?(value) ⇒ Boolean
16 17 18 |
# File 'lib/tty/conversion/converter/boolean.rb', line 16 def boolean?(value) target.any? { |klass| value.is_a?(klass) } end |
#convert(value) ⇒ Object
Convert value to boolean type including range of strings such as
other values coerced to false are:
0, f, F, FALSE, false, False, n, N, No, no, No
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/tty/conversion/converter/boolean.rb', line 37 def convert(value) case value.to_s when /^(yes|y|t(rue)?|1)$/i return true when /^(no|n|f(alse)?|0)$/i return false else fail TypeError, "Expected boolean type, got #{value}" end end |