class AMA::Entity::Mapper::Handler::Entity::Factory
Default entity factory
Constants
- INSTANCE
Private Class Methods
handler_factory(implementation, fallback)
click to toggle source
@param [Factory] implementation @param [Factory] fallback @return [Factory]
# File lib/ama-entity-mapper/handler/entity/factory.rb, line 67 def handler_factory(implementation, fallback) lambda do |type, data, ctx| begin implementation.create(type, data, ctx) do |t, d, c| fallback.create(t, d, c) end rescue StandardError => e raise_if_internal(e) message = "Unexpected error from factory #{implementation}" signature = '(type, data, context)' options = { parent: e, context: ctx, signature: signature } compliance_error(message, options) end end end
wrap(implementation)
click to toggle source
@param [Factory] implementation @return [Factory]
# File lib/ama-entity-mapper/handler/entity/factory.rb, line 52 def wrap(implementation) handler = handler_factory(implementation, INSTANCE) description = "Safety wrapper for #{implementation}" wrapper = method_object(:create, to_s: description, &handler) wrapper.singleton_class.instance_eval do include Mixin::Errors end wrapper end
Public Instance Methods
create(type, _data, context)
click to toggle source
@param [AMA::Entity::Mapper::Type] type @param [Object] _data @param [AMA::Entity::Mapper::Context] context
# File lib/ama-entity-mapper/handler/entity/factory.rb, line 21 def create(type, _data, context) create_internal(type, context) rescue StandardError => e message = "Failed to instantiate #{type} directly from class" if e.is_a?(ArgumentError) message += '. Does it have parameterless #initialize() method?' end mapping_error(message, parent: e, context: context) end
Private Instance Methods
create_internal(type, context)
click to toggle source
@param [AMA::Entity::Mapper::Type] type @param [AMA::Entity::Mapper::Context] context
# File lib/ama-entity-mapper/handler/entity/factory.rb, line 35 def create_internal(type, context) entity = type.type.new type.attributes.values.each do |attribute| next if attribute.default.nil? || attribute.virtual segment = Path::Segment.attribute(attribute.name) ctx = context.advance(segment) value = attribute.default type.injector.inject(entity, type, attribute, value, ctx) end entity end