Module: RequestParamsValidation::Params::Converter

Extended by:
Types::Conversions
Defined in:
lib/request_params_validation/params/converter.rb

Class Method Summary collapse

Methods included from Types::Conversions

convert_to_boolean, convert_to_date, convert_to_datetime, convert_to_decimal, convert_to_integer, convert_to_string

Class Method Details

.apply_transformation(param, value) ⇒ Object



24
25
26
27
28
# File 'lib/request_params_validation/params/converter.rb', line 24

def self.apply_transformation(param, value)
  transform = param.transform

  transform.respond_to?(:call) ? transform.call(value) : value.send(transform)
end

.coerce(param, value) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/request_params_validation/params/converter.rb', line 8

def self.coerce(param, value)
  type = param.type

  method_name = "convert_to_#{type}"

  return value unless self.respond_to?(method_name)

  if [Params::DATE_TYPE, Params::DATETIME_TYPE].include?(type)
    self.send(method_name, value, param.format.try(:strptime))
  elsif type == Params::DECIMAL_TYPE
    self.send(method_name, value, param.decimal_precision)
  else
    self.send(method_name, value)
  end
end