Sha256: 834b668a42ff3c5a15da3d48ab2a6fe53608e9907548b20015d2c3a82c802415

Contents?: true

Size: 1.27 KB

Versions: 18

Compression:

Stored size: 1.27 KB

Contents

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

      # @param [String, Object] input
      # @return [nil] if the input is an empty string
      # @return [Object] otherwise the input object is returned
      def to_nil(input)
        input unless empty_str?(input)
      end

      # @param [#to_str, Object] input
      # @return [Date, Object]
      # @see Date.parse
      def to_date(input)
        return input unless input.respond_to?(:to_str)
        Date.parse(input)
      rescue ArgumentError, RangeError
        input
      end

      # @param [#to_str, Object] input
      # @return [DateTime, Object]
      # @see DateTime.parse
      def to_date_time(input)
        return input unless input.respond_to?(:to_str)
        DateTime.parse(input)
      rescue ArgumentError
        input
      end

      # @param [#to_str, Object] input
      # @return [Time, Object]
      # @see Time.parse
      def to_time(input)
        return input unless input.respond_to?(:to_str)
        Time.parse(input)
      rescue ArgumentError
        input
      end

      private

      # Checks whether String is empty
      # @param [String, Object] value
      # @return [Boolean]
      def empty_str?(value)
        EMPTY_STRING.eql?(value)
      end
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
dry-types-0.14.1 lib/dry/types/coercions.rb
dry-types-0.15.0 lib/dry/types/coercions.rb
dry-types-0.14.0 lib/dry/types/coercions.rb
dry-types-0.13.4 lib/dry/types/coercions.rb
dry-types-0.13.3 lib/dry/types/coercions.rb
dry-types-0.12.3 lib/dry/types/coercions.rb
dry-types-0.13.2 lib/dry/types/coercions.rb
dry-types-0.13.1 lib/dry/types/coercions.rb
dry-types-0.13.0 lib/dry/types/coercions.rb
dry-types-0.12.2 lib/dry/types/coercions.rb
dry-types-0.12.1 lib/dry/types/coercions.rb
dry-types-0.12.0 lib/dry/types/coercions.rb
dry-types-0.11.1 lib/dry/types/coercions.rb
dry-types-0.11.0 lib/dry/types/coercions.rb
dry-types-0.10.3 lib/dry/types/coercions.rb
dry-types-0.10.2 lib/dry/types/coercions.rb
dry-types-0.10.1 lib/dry/types/coercions.rb
dry-types-0.10.0 lib/dry/types/coercions.rb