Total Complexity | 4 |
Total Lines | 31 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | # frozen_string_literal: true |
||
13 | class Factory < API::Factory |
||
14 | include Mixin::Errors |
||
15 | |||
16 | INSTANCE = new |
||
17 | |||
18 | # @param [AMA::Entity::Mapper::Type] type |
||
19 | # @param [Object] _data |
||
20 | # @param [AMA::Entity::Mapper::Context] context |
||
21 | def create(type, _data, context) |
||
22 | create_internal(type) |
||
23 | rescue StandardError => e |
||
24 | message = "Failed to instantiate #{type} directly from class" |
||
25 | if e.is_a?(ArgumentError) |
||
26 | message += '. Does it have parameterless #initialize() method?' |
||
27 | end |
||
28 | mapping_error(message, parent: e, context: context) |
||
29 | end |
||
30 | |||
31 | private |
||
32 | |||
33 | def create_internal(type) |
||
34 | entity = type.type.new |
||
35 | type.attributes.values.each do |attribute| |
||
36 | next if attribute.default.nil? || attribute.virtual |
||
37 | segment = Path::Segment.attribute(attribute.name) |
||
38 | value = attribute.default |
||
39 | type.injector.inject(entity, type, attribute, value, segment) |
||
40 | end |
||
41 | entity |
||
42 | end |
||
43 | end |
||
44 | end |
||
49 |