Class: AttrTypecastable::Types::Boolean

Inherits:
Base
  • Object
show all
Defined in:
lib/attr_typecastable/types/boolean.rb

Instance Method Summary collapse

Methods inherited from Base

#typecast

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(**options)
  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