Class: TypedEAV::Field::Boolean

Inherits:
Base show all
Defined in:
app/models/typed_eav/field/boolean.rb

Constant Summary

Constants inherited from Base

TypedEAV::Field::Base::RESERVED_NAMES

Constants included from ColumnMapping

ColumnMapping::DEFAULT_OPERATORS_BY_COLUMN, ColumnMapping::FALLBACK_OPERATORS

Instance Method Summary collapse

Methods inherited from Base

#allowed_option_values, #array_field?, #clear_option_cache!, #default_value, #default_value=, #field_type_name, #optionable?, #validate_typed_value

Instance Method Details

#cast(raw) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'app/models/typed_eav/field/boolean.rb', line 9

def cast(raw)
  return [nil, false] if raw.nil?
  return [nil, false] if raw.is_a?(String) && raw.strip.empty?

  recognized = %w[true false 1 0 t f yes no on off].freeze
  unless raw == true || raw == false || raw == 0 || raw == 1 || recognized.include?(raw.to_s.strip.downcase) # rubocop:disable Style/NumericPredicate
    return [nil, true]
  end

  [ActiveModel::Type::Boolean.new.cast(raw), false]
end