Class: AttrTypecastable::Types::Boolean
- Defined in:
- lib/attr_typecastable/types/boolean.rb
Instance Method Summary collapse
- #do_typecast(value) ⇒ Object
-
#initialize(**options) ⇒ Boolean
constructor
A new instance of Boolean.
Methods inherited from Base
Constructor Details
#initialize(**options) ⇒ Boolean
Returns a new instance of Boolean.
7 8 9 10 11 |
# File 'lib/attr_typecastable/types/boolean.rb', line 7 def initialize(**) super @options[:true_values] ||= ["true", 1] @options[:false_values] ||= ["false", 0] end |
Instance Method Details
#do_typecast(value) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/attr_typecastable/types/boolean.rb', line 13 def do_typecast(value) return value if value.is_a?(::TrueClass) || value.is_a?(::FalseClass) true_values = Array(@options[:true_values]) false_values = Array(@options[:false_values]) if true_values.any? {|v| v === value } true elsif false_values.any? {|v| v === value } false else !!value end end |