module Dry::Data::Coercions::Form

Constants

BOOLEAN_MAP
FALSE_VALUES
TRUE_VALUES

Public Class Methods

to_date(input) click to toggle source
# File lib/dry/data/coercions/form.rb, line 21
def self.to_date(input)
  Date.parse(input)
rescue ArgumentError
  input
end
to_date_time(input) click to toggle source
# File lib/dry/data/coercions/form.rb, line 27
def self.to_date_time(input)
  DateTime.parse(input)
rescue ArgumentError
  input
end
to_decimal(input) click to toggle source
# File lib/dry/data/coercions/form.rb, line 75
def self.to_decimal(input)
  if input == ''
    nil
  else
    result = to_float(input)

    if result.is_a?(Float)
      result.to_d
    else
      result
    end
  end
end
to_false(input) click to toggle source
# File lib/dry/data/coercions/form.rb, line 43
def self.to_false(input)
  BOOLEAN_MAP.fetch(input, input)
end
to_float(input) click to toggle source
# File lib/dry/data/coercions/form.rb, line 61
def self.to_float(input)
  if input == ''
    nil
  else
    result = input.to_f

    if result == 0.0 && (input != '0' || input != '0.0')
      input
    else
      result
    end
  end
end
to_int(input) click to toggle source
# File lib/dry/data/coercions/form.rb, line 47
def self.to_int(input)
  if input == ''
    nil
  else
    result = input.to_i

    if result === 0 && input != '0'
      input
    else
      result
    end
  end
end
to_nil(input) click to toggle source
# File lib/dry/data/coercions/form.rb, line 13
def self.to_nil(input)
  if input.is_a?(String) && input == ''
    nil
  else
    input
  end
end
to_time(input) click to toggle source
# File lib/dry/data/coercions/form.rb, line 33
def self.to_time(input)
  Time.parse(input)
rescue ArgumentError
  input
end
to_true(input) click to toggle source
# File lib/dry/data/coercions/form.rb, line 39
def self.to_true(input)
  BOOLEAN_MAP.fetch(input, input)
end