Sha256: f832c643bc3aabd93878425ae3a30cf44f609451c74f05a09efddf6cf9766921

Contents?: true

Size: 1.97 KB

Versions: 2

Compression:

Stored size: 1.97 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

        # @param [String, Object] input
        # @return [Boolean?]
        # @see TRUE_VALUES
        # @see FALSE_VALUES
        def self.to_true(input)
          BOOLEAN_MAP.fetch(input.to_s, input)
        end

        # @param [String, Object] input
        # @return [Boolean?]
        # @see TRUE_VALUES
        # @see FALSE_VALUES
        def self.to_false(input)
          BOOLEAN_MAP.fetch(input.to_s, input)
        end

        # @param [#to_int, #to_i, Object] input
        # @return [Integer?, Object]
        def self.to_int(input)
          if empty_str?(input)
            nil
          else
            Integer(input)
          end
        rescue ArgumentError, TypeError
          input
        end

        # @param [#to_f, Object] input
        # @return [Float?, Object]
        def self.to_float(input)
          if empty_str?(input)
            nil
          else
            Float(input)
          end
        rescue ArgumentError, TypeError
          input
        end

        # @param [#to_d, Object] input
        # @return [BigDecimal?, Object]
        def self.to_decimal(input)
          result = to_float(input)

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

        # @param [Array, '', Object] input
        # @return [Array, Object]
        def self.to_ary(input)
          empty_str?(input) ? [] : input
        end

        # @param [Hash, '', Object] input
        # @return [Hash]
        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.4 lib/dry/types/coercions/form.rb
dry-types-0.9.3 lib/dry/types/coercions/form.rb