Sha256: d14fe5de4fffb7fdc877b52d272bcaa5f90c86f431adce39c11a59fc8e809fe5

Contents?: true

Size: 774 Bytes

Versions: 3

Compression:

Stored size: 774 Bytes

Contents

require 'dry/core/constants'

module Dry
  module Types
    module Coercions
      include Dry::Core::Constants

      def to_nil(input)
        input unless empty_str?(input)
      end

      def to_date(input)
        return input unless input.respond_to?(:to_str)
        Date.parse(input)
      rescue ArgumentError
        input
      end

      def to_date_time(input)
        return input unless input.respond_to?(:to_str)
        DateTime.parse(input)
      rescue ArgumentError
        input
      end

      def to_time(input)
        return input unless input.respond_to?(:to_str)
        Time.parse(input)
      rescue ArgumentError
        input
      end

      private

      def empty_str?(value)
        EMPTY_STRING.eql?(value)
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
dry-types-0.9.2 lib/dry/types/coercions.rb
dry-types-0.9.1 lib/dry/types/coercions.rb
dry-types-0.9.0 lib/dry/types/coercions.rb