class AMA::Entity::Mapper::Handler::Entity::Enumerator

Default attribute enumerator

Constants

INSTANCE

Public Class Methods

wrap(implementation) click to toggle source

@param [Enumerator] implementation @return [Enumerator]

# File lib/ama-entity-mapper/handler/entity/enumerator.rb, line 39
def wrap(implementation)
  handler = handler_factory(implementation, INSTANCE)
  description = "Safety wrapper for #{implementation}"
  wrapper = method_object(:enumerate, to_s: description, &handler)
  wrapper.singleton_class.instance_eval do
    include Mixin::Errors
  end
  wrapper
end

Private Class Methods

handler_factory(implementation, fallback) click to toggle source

@param [Enumerator] implementation @param [Enumerator] fallback @return [Enumerator]

# File lib/ama-entity-mapper/handler/entity/enumerator.rb, line 54
def handler_factory(implementation, fallback)
  lambda do |entity, type, ctx|
    begin
      implementation.enumerate(entity, type, ctx) do |e, t, c|
        fallback.enumerate(e, t, c)
      end
    rescue StandardError => e
      raise_if_internal(e)
      message = 'Unexpected error from enumerator ' \
        "#{implementation}"
      signature = '(entity, type, context)'
      options = { parent: e, context: ctx, signature: signature }
      compliance_error(message, **options)
    end
  end
end

Public Instance Methods

enumerate(entity, type, _context) click to toggle source

@param [Object] entity @param [AMA::Entity::Mapper::Type] type @param [AMA::Entity::Mapper::Context] _context

# File lib/ama-entity-mapper/handler/entity/enumerator.rb, line 20
def enumerate(entity, type, _context)
  ::Enumerator.new do |yielder|
    type.attributes.values.each do |attribute|
      next if attribute.virtual
      value = attribute.default
      if object_variable_exists(entity, attribute.name)
        value = object_variable(entity, attribute.name)
      end
      segment = Path::Segment.attribute(attribute.name)
      yielder << [attribute, value, segment]
    end
  end
end