Sha256: dc15f2385e8c32695e72e48e83879f12af31e5b082f46aa0ed651dd64d73b146
Contents?: true
Size: 1.85 KB
Versions: 1
Compression:
Stored size: 1.85 KB
Contents
require 'date' require 'bigdecimal' require 'bigdecimal/util' require 'time' module Dry module Types module Coercions module Form TRUE_VALUES = %w[1 on t true y yes].freeze FALSE_VALUES = %w[0 off f false n no].freeze BOOLEAN_MAP = ::Hash[TRUE_VALUES.product([true]) + FALSE_VALUES.product([false])].freeze def self.to_nil(input) if input.is_a?(String) && input == '' nil else input end end def self.to_date(input) Date.parse(input) rescue ArgumentError input end def self.to_date_time(input) DateTime.parse(input) rescue ArgumentError input end def self.to_time(input) Time.parse(input) rescue ArgumentError input end def self.to_true(input) BOOLEAN_MAP.fetch(input, input) end def self.to_false(input) BOOLEAN_MAP.fetch(input, input) end def self.to_int(input) if input == '' nil else result = input.to_i if result === 0 && input != '0' input else result end end end 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 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 end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dry-types-0.7.0 | lib/dry/types/coercions/form.rb |