1
|
|
|
# frozen_string_literal: true |
2
|
|
|
|
3
|
|
|
require_relative '../denormalizer' |
4
|
|
|
require_relative '../default/denormalizer' |
5
|
|
|
require_relative '../../mixin/errors' |
6
|
|
|
require_relative '../../context' |
7
|
|
|
|
8
|
|
|
module AMA |
9
|
|
|
module Entity |
10
|
|
View Code Duplication |
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
|
|
|
|