|
1
|
|
|
# frozen_string_literal: true |
|
2
|
|
|
|
|
3
|
|
|
require_relative '../mixin/errors' |
|
4
|
|
|
require_relative '../context' |
|
5
|
|
|
|
|
6
|
|
|
module AMA |
|
7
|
|
|
module Entity |
|
8
|
|
|
class Mapper |
|
9
|
|
|
class Engine |
|
10
|
|
|
# Thin denormalization master. Delegates processing to type |
|
11
|
|
|
# denormalizer and adds security wrap. |
|
12
|
|
|
class Denormalizer |
|
13
|
|
|
include ::AMA::Entity::Mapper::Mixin::Errors |
|
14
|
|
|
|
|
15
|
|
|
# @param [Object] source |
|
16
|
|
|
# @param [AMA::Entity::Mapper::Type] target_type |
|
17
|
|
|
# @param [AMA::Entity::Mapper::Context] context |
|
18
|
|
|
# @return [Object] Object of target type |
|
19
|
|
|
def denormalize(source, target_type, context) |
|
20
|
|
|
result = denormalize_internal(source, target_type, context) |
|
21
|
|
|
return result if target_type.instance?(result) |
|
22
|
|
|
message = "Denormalizer for type #{target_type} has returned " \ |
|
23
|
|
|
"something that is not an instance of #{target_type}: " \ |
|
24
|
|
|
"#{result} (#{result.class})" |
|
25
|
|
|
compliance_error(message, context: context) |
|
26
|
|
|
end |
|
27
|
|
|
|
|
28
|
|
|
private |
|
29
|
|
|
|
|
30
|
|
|
# @param [Object] source |
|
31
|
|
|
# @param [AMA::Entity::Mapper::Type] target_type |
|
32
|
|
|
# @param [AMA::Entity::Mapper::Context] context |
|
33
|
|
|
# @return [Object] Object of target type |
|
34
|
|
View Code Duplication |
def denormalize_internal(source, target_type, context) |
|
|
|
|
|
|
35
|
|
|
denormalizer = target_type.denormalizer |
|
36
|
|
|
denormalizer.denormalize(source, target_type, context) |
|
37
|
|
|
rescue StandardError => e |
|
38
|
|
|
raise_if_internal(e) |
|
39
|
|
|
message = "Error while denormalizing #{target_type} " \ |
|
40
|
|
|
"out of #{source.class}" |
|
41
|
|
|
mapping_error(message, parent: e, context: context) |
|
42
|
|
|
end |
|
43
|
|
|
end |
|
44
|
|
|
end |
|
45
|
|
|
end |
|
46
|
|
|
end |
|
47
|
|
|
end |
|
48
|
|
|
|