Sha256: 8695b4f80b9cb8af7e60008792f45ed3aabd3c51a1bab69df2743afe6bdbe8bf

Contents?: true

Size: 670 Bytes

Versions: 4

Compression:

Stored size: 670 Bytes

Contents

module Compel
  module Coercion

    class DateTime < Type

      attr_reader :format

      def coerce_value
        @format = options[:format] || '%FT%T'

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

        coerced = ::DateTime.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 datetime 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/datetime.rb
compel-0.3.4 lib/compel/coercion/types/datetime.rb
compel-0.3.2 lib/compel/coercion/types/datetime.rb
compel-0.3.1 lib/compel/coercion/types/datetime.rb