Sha256: d703c697c4397690e6bed8fe88451bc28b471509b697fab4e336fa18a73868f5

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require 'date'
require 'bigdecimal'
require 'bigdecimal/util'

module Dry
  module Data
    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_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-data-0.1.0 lib/dry/data/coercions/form.rb