| @@ 10-44 (lines=35) @@ | ||
| 7 | ||
| 8 | module AMA |
|
| 9 | module Entity |
|
| 10 | class Mapper |
|
| 11 | module API |
|
| 12 | module Wrapper |
|
| 13 | # Factory safety wrapper |
|
| 14 | class Factory < API::Factory |
|
| 15 | include Mixin::Errors |
|
| 16 | ||
| 17 | # @param [AMA::Entity::Mapper::API::Factory] processor |
|
| 18 | def initialize(processor) |
|
| 19 | @processor = processor |
|
| 20 | @fallback = Default::Factory::INSTANCE |
|
| 21 | end |
|
| 22 | ||
| 23 | # @param [AMA::Entity::Mapper::Type] type |
|
| 24 | # @param [Object] data |
|
| 25 | # @param [AMA::Entity::Mapper::Context] context |
|
| 26 | def create(type, data = nil, context = nil) |
|
| 27 | context ||= Context.new |
|
| 28 | @processor.create(type, data, context) do |t, d, c| |
|
| 29 | @fallback.create(t, d, c) |
|
| 30 | end |
|
| 31 | rescue StandardError => e |
|
| 32 | raise_if_internal(e) |
|
| 33 | message = "Error while creatin #{type} instance " \ |
|
| 34 | "(type: #{type}) attributes using #{@processor}" |
|
| 35 | if e.is_a?(ArgumentError) |
|
| 36 | message += "Does #{@processor}#create have signature " \ |
|
| 37 | '(entity, type, context = nil)?' |
|
| 38 | end |
|
| 39 | mapping_error(message, parent: e, context: context) |
|
| 40 | end |
|
| 41 | end |
|
| 42 | end |
|
| 43 | end |
|
| 44 | end |
|
| 45 | end |
|
| 46 | end |
|
| 47 | ||
| @@ 10-44 (lines=35) @@ | ||
| 7 | ||
| 8 | module AMA |
|
| 9 | module Entity |
|
| 10 | class Mapper |
|
| 11 | module API |
|
| 12 | module Wrapper |
|
| 13 | # Default attribute injector |
|
| 14 | class Normalizer < API::Normalizer |
|
| 15 | include Mixin::Errors |
|
| 16 | ||
| 17 | # @param [AMA::Entity::Mapper::API::Normalizer] processor |
|
| 18 | def initialize(processor) |
|
| 19 | @processor = processor |
|
| 20 | @fallback = Default::Normalizer::INSTANCE |
|
| 21 | end |
|
| 22 | ||
| 23 | # @param [Object] entity |
|
| 24 | # @param [AMA::Entity::Mapper::Type] type |
|
| 25 | # @param [AMA::Entity::Mapper::Context] context |
|
| 26 | def normalize(entity, type, context = nil) |
|
| 27 | ctx = context || Context.new |
|
| 28 | @processor.normalize(entity, type, ctx) do |e, t, c| |
|
| 29 | @fallback.normalize(e, t, c) |
|
| 30 | end |
|
| 31 | rescue StandardError => e |
|
| 32 | raise_if_internal(e) |
|
| 33 | message = "Error while normalizing #{entity} (type: #{type}) " \ |
|
| 34 | "using #{@processor}" |
|
| 35 | if e.is_a?(ArgumentError) |
|
| 36 | message += "Does #{@processor}#normalize have signature " \ |
|
| 37 | '(entity, type, context = nil)?' |
|
| 38 | end |
|
| 39 | mapping_error(message, parent: e, context: ctx) |
|
| 40 | end |
|
| 41 | end |
|
| 42 | end |
|
| 43 | end |
|
| 44 | end |
|
| 45 | end |
|
| 46 | end |
|
| 47 | ||
| @@ 10-44 (lines=35) @@ | ||
| 7 | ||
| 8 | module AMA |
|
| 9 | module Entity |
|
| 10 | class Mapper |
|
| 11 | module API |
|
| 12 | module Wrapper |
|
| 13 | # Denormalizer safety wrapper |
|
| 14 | class Denormalizer < API::Denormalizer |
|
| 15 | include Mixin::Errors |
|
| 16 | ||
| 17 | # @param [AMA::Entity::Mapper::API::Denormalizer] processor |
|
| 18 | def initialize(processor) |
|
| 19 | @processor = processor |
|
| 20 | @fallback = Default::Denormalizer::INSTANCE |
|
| 21 | end |
|
| 22 | ||
| 23 | # @param [Hash] source |
|
| 24 | # @param [AMA::Entity::Mapper::Type] type |
|
| 25 | # @param [AMA::Entity::Mapper::Context] context |
|
| 26 | def denormalize(source, type, context = nil) |
|
| 27 | ctx = context || Context.new |
|
| 28 | @processor.denormalize(source, type, ctx) do |s, t, c| |
|
| 29 | @fallback.denormalize(s, t, c) |
|
| 30 | end |
|
| 31 | rescue StandardError => e |
|
| 32 | raise_if_internal(e) |
|
| 33 | message = "Error while denormalizing #{type} " \ |
|
| 34 | "(type: #{type}) from #{source.class} using #{@processor}" |
|
| 35 | if e.is_a?(ArgumentError) |
|
| 36 | message += "Does #{@processor}#denormalize have signature " \ |
|
| 37 | '(entity, source, type, context = nil)?' |
|
| 38 | end |
|
| 39 | mapping_error(message, parent: e, context: ctx) |
|
| 40 | end |
|
| 41 | end |
|
| 42 | end |
|
| 43 | end |
|
| 44 | end |
|
| 45 | end |
|
| 46 | end |
|
| 47 | ||
| @@ 10-44 (lines=35) @@ | ||
| 7 | ||
| 8 | module AMA |
|
| 9 | module Entity |
|
| 10 | class Mapper |
|
| 11 | module API |
|
| 12 | module Wrapper |
|
| 13 | # Denormalizer safety wrapper |
|
| 14 | class Enumerator < API::Enumerator |
|
| 15 | include Mixin::Errors |
|
| 16 | ||
| 17 | # @param [AMA::Entity::Mapper::API::Enumerator] processor |
|
| 18 | def initialize(processor) |
|
| 19 | @processor = processor |
|
| 20 | @fallback = Default::Enumerator::INSTANCE |
|
| 21 | end |
|
| 22 | ||
| 23 | # @param [Object] entity |
|
| 24 | # @param [AMA::Entity::Mapper::Type] type |
|
| 25 | # @param [AMA::Entity::Mapper::Context] context |
|
| 26 | def enumerate(entity, type, context = nil) |
|
| 27 | context ||= Context.new |
|
| 28 | @processor.enumerate(entity, type, context) do |e, t, c| |
|
| 29 | @fallback.enumerate(e, t, c) |
|
| 30 | end |
|
| 31 | rescue StandardError => e |
|
| 32 | raise_if_internal(e) |
|
| 33 | message = "Error while enumerating #{entity.class} " \ |
|
| 34 | "(type: #{type}) attributes using #{@processor}" |
|
| 35 | if e.is_a?(ArgumentError) |
|
| 36 | message += "Does #{@processor}#enumerate have signature " \ |
|
| 37 | '(entity, type, context = nil)?' |
|
| 38 | end |
|
| 39 | mapping_error(message, parent: e, context: context) |
|
| 40 | end |
|
| 41 | end |
|
| 42 | end |
|
| 43 | end |
|
| 44 | end |
|
| 45 | end |
|
| 46 | end |
|
| 47 | ||