Sha256: 53a537b4244612f400ed5422989df3249f83f557149a668bd9d1cd685bd46505

Contents?: true

Size: 654 Bytes

Versions: 4

Compression:

Stored size: 654 Bytes

Contents

module Compel
  module Coercion

    class Time < Type

      attr_reader :format

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

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

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