Sha256: 5cc862b49d3f973efeb3f30e313ba8e46e1df0058f457290b55dcb6f6dcc1190

Contents?: true

Size: 1.19 KB

Versions: 1

Compression:

Stored size: 1.19 KB

Contents

require 'bigdecimal'
require 'bigdecimal/util'

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

        extend Coercions

        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)
          return if empty_str?(input)

          result = input.to_i

          if result === 0 && !input.eql?('0')
            input
          else
            result
          end
        end

        def self.to_float(input)
          return if empty_str?(input)

          result = input.to_f

          if result.eql?(0.0) && (!input.eql?('0') && !input.eql?('0.0'))
            input
          else
            result
          end
        end

        def self.to_decimal(input)
          result = to_float(input)

          if result.instance_of?(Float)
            result.to_d
          else
            result
          end
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
dry-types-0.7.1 lib/dry/types/coercions/form.rb