Module: SmoothOperator::TypeCasting

Extended by:
TypeCasting
Included in:
TypeCasting
Defined in:
lib/smooth_operator/type_casting.rb

Constant Summary collapse

TRUE_VALUES =

RIPPED FROM RAILS

[true, 1, '1', 't', 'T', 'true', 'TRUE', 'on', 'ON'].to_set
FALSE_VALUES =
[false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].to_set

Instance Method Summary collapse

Instance Method Details

#cast_to_type(name, value, parent_object) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/smooth_operator/type_casting.rb', line 10

def cast_to_type(name, value, parent_object)
  type, known_attribute, unknown_hash_class = extract_args(parent_object.class, name)

  return Helpers.duplicate(value) if known_attribute && type.nil?

  case value
  when Array
    value.map { |array_entry| cast_to_type(name, array_entry, parent_object) }
  when Hash
    type.nil? ? new_unknown_hash(value, unknown_hash_class, parent_object) : type.new(value, parent_object: parent_object)
  else
    convert(value, type)
  end
end