Sha256: 434c56a281fba809f4c590569acfe6c76beb743c8613d0ea1e3d18f9cde23f8f

Contents?: true

Size: 747 Bytes

Versions: 4

Compression:

Stored size: 747 Bytes

Contents

module Compel
  module Coercion

    class DateType < Type

      attr_reader :format

      def coerce_value
        @format = default_format

        if options[:format]
          @format = options[:format][:value]
        end

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

        coerced = klass.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 #{klass.to_s.downcase} 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.5.1 lib/compel/coercion/types/date_type.rb
compel-0.5.0 lib/compel/coercion/types/date_type.rb
compel-0.4.3 lib/compel/coercion/types/date_type.rb
compel-0.4.2 lib/compel/coercion/types/date_type.rb