Module: ValidateMyRoutes::Validate::ConvertToType

Defined in:
lib/validate_my_routes/validate/convert_to_type.rb

Overview

ConvertToType module provides single method convert_to_type to convert value into type to_type. Conversion can fail with InvalidTypeError.

Constant Summary collapse

Boolean =

rubocop:disable Naming/ConstantName

:Boolean
SIMPLE_TYPES =
[Float, String, Date, Time, DateTime, Integer].freeze
COMPOSITE_TYPES =
[Array, Hash].freeze
BOOLEAN_TYPES =
[Boolean, TrueClass, FalseClass].freeze

Class Method Summary collapse

Class Method Details

.convert_to_type(value, to_type) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/validate_my_routes/validate/convert_to_type.rb', line 14

def convert_to_type(value, to_type)
  return value if already_of_type?(value, to_type)

  if SIMPLE_TYPES.include?(to_type)
    parse_simple_type(value, to_type)
  elsif COMPOSITE_TYPES.include?(to_type)
    parse_composite_type(value, to_type)
  elsif BOOLEAN_TYPES.include?(to_type)
    parse_boolean(value)
  else
    raise_unknown_type(to_type)
  end
rescue ArgumentError
  raise_with_invalid_type(value, to_type)
end