Sha256: a33ebfb870bf47c9f66982aea20810d20250879ba310153c825a7be236bb458c

Contents?: true

Size: 1013 Bytes

Versions: 10

Compression:

Stored size: 1013 Bytes

Contents

# Represents a plain old boolean type. TBD: can be nil?
#
require_relative '../exceptions'

require 'date'

module Attributor

  class DateTime < Temporal

    def self.native_type
      return ::DateTime
    end

    def self.example(context=nil, options: {})
      return self.load(/[:date:]/.gen, context)
    end

    def self.load(value,context=Attributor::DEFAULT_ROOT_CONTEXT, **options)
      # We assume that if the value is already in the right type, we've decoded it already
      return value if value.is_a?(self.native_type)
      return value.to_datetime if value.respond_to?(:to_datetime)
      return nil unless value.is_a?(::String)
      # TODO: we should be able to convert not only from String but Time...etc
      # Else, we'll decode it from String.
      begin
        return ::DateTime.parse(value)
      rescue ArgumentError => e
        raise Attributor::DeserializationError, context: context, from: value.class, encoding: "DateTime" , value: value
      end
    end

    


  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
attributor-5.0.2 lib/attributor/types/date_time.rb
attributor-5.0.1 lib/attributor/types/date_time.rb
attributor-5.0 lib/attributor/types/date_time.rb
attributor-4.2.0 lib/attributor/types/date_time.rb
attributor-4.1.0 lib/attributor/types/date_time.rb
attributor-4.0.1 lib/attributor/types/date_time.rb
attributor-4.0.0 lib/attributor/types/date_time.rb
attributor-3.0.1 lib/attributor/types/date_time.rb
attributor-3.0 lib/attributor/types/date_time.rb
attributor-2.6.1 lib/attributor/types/date_time.rb