Sha256: 42e0e6d68e230ad2c9f3b85b26f03668d76fb3447313875c1e80c7b666f9d664
Contents?: true
Size: 1.35 KB
Versions: 2
Compression:
Stored size: 1.35 KB
Contents
require 'bigdecimal' require 'bigdecimal/util' module Dry module Types module Coercions module Form TRUE_VALUES = %w[1 on On ON t true True TRUE T y yes Yes YES].freeze FALSE_VALUES = %w[0 off Off OFF f false False FALSE F n no No NO].freeze BOOLEAN_MAP = ::Hash[TRUE_VALUES.product([true]) + FALSE_VALUES.product([false])].freeze extend Coercions def self.to_true(input) BOOLEAN_MAP.fetch(input.to_s, input) end def self.to_false(input) BOOLEAN_MAP.fetch(input.to_s, input) end def self.to_int(input) if empty_str?(input) nil else Integer(input) end rescue ArgumentError, TypeError input end def self.to_float(input) if empty_str?(input) nil else Float(input) end rescue ArgumentError, TypeError input end def self.to_decimal(input) result = to_float(input) if result.instance_of?(Float) input.to_d else result end end def self.to_ary(input) empty_str?(input) ? [] : input end def self.to_hash(input) empty_str?(input) ? {} : input end end end end end
Version data entries
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
dry-types-0.9.1 | lib/dry/types/coercions/form.rb |
dry-types-0.9.0 | lib/dry/types/coercions/form.rb |