class AMA::Entity::Mapper::Type::BuiltIn::DateTimeType

DateTime type description

Constants

INSTANCE

Public Class Methods

new() click to toggle source
Calls superclass method AMA::Entity::Mapper::Type::new
# File lib/ama-entity-mapper/type/builtin/datetime_type.rb, line 15
def initialize
  super(DateTime)

  normalizer_block do |entity, *|
    entity.iso8601(3)
  end

  define_denormalizer
  define_factory

  enumerator_block do |*|
    ::Enumerator.new { |*| }
  end

  injector_block { |*| }
end

Private Instance Methods

define_denormalizer() click to toggle source
# File lib/ama-entity-mapper/type/builtin/datetime_type.rb, line 34
def define_denormalizer
  denormalizer_block do |input, _, ctx|
    break input if input.is_a?(DateTime)
    input = input.to_s if input.is_a?(Symbol)
    break DateTime.iso8601(input, 3) if input.is_a?(String)
    if input.is_a?(Integer)
      break DateTime.strptime(input.to_s, '%s')
    end
    singleton_class.send(:include, Mixin::Errors)
    message = 'String input expected (like ' \
      "'2001-02-03T04:05:06.123+04:00'), " \
      "#{input.class} received: #{input}"
    mapping_error(message, context: ctx)
  end
end
define_factory() click to toggle source
# File lib/ama-entity-mapper/type/builtin/datetime_type.rb, line 50
def define_factory
  factory_block do |_, _, ctx|
    singleton_class.send(:include, Mixin::Errors)
    message = 'DateTime type could not be instantiated directly, ' \
      'it only supports normalization and denormalization'
    compliance_error(message, context: ctx)
  end
end