Sha256: 6aaacabd29062d873787f1316527ce14517a35baf92293013923156d06bc1d4b
Contents?: true
Size: 1.35 KB
Versions: 1
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 Y].freeze FALSE_VALUES = %w[0 off Off OFF f false False FALSE F n no No NO N].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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
dry-types-0.9.2 | lib/dry/types/coercions/form.rb |