Sha256: 03b93dd69b45cf24ac9d612727335be35da4b9e411ffa088ade9e3a4ac046b60

Contents?: true

Size: 657 Bytes

Versions: 4

Compression:

Stored size: 657 Bytes

Contents

module Compel
  module Coercion

    class Date < Type

      attr_reader :format

      def coerce_value
        @format = options[:format] || '%Y-%m-%d'

        if value.is_a?(::Date)
          @value = value.strftime(format)
        end

        coerced = ::Date.strptime(value, format)

        if coerced.strftime(format) == value
          return coerced
        end

        build_error_result

        rescue
          build_error_result
      end

      def build_error_result
        custom_error = "'#{value}' is not a parsable date with format: #{format}"

        Result.new(nil, value, self.class, custom_error)
      end

    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
compel-0.3.6 lib/compel/coercion/types/date.rb
compel-0.3.4 lib/compel/coercion/types/date.rb
compel-0.3.2 lib/compel/coercion/types/date.rb
compel-0.3.1 lib/compel/coercion/types/date.rb