Class: PackAPI::Mapping::ValueObjectFactory
- Inherits:
-
Object
- Object
- PackAPI::Mapping::ValueObjectFactory
- Defined in:
- lib/pack_api/mapping/value_object_factory.rb
Class Attribute Summary collapse
-
.attribute_map_registry ⇒ Object
readonly
Returns the value of attribute attribute_map_registry.
-
.value_object_attributes ⇒ Object
readonly
Returns the value of attribute value_object_attributes.
Class Method Summary collapse
- .model_attributes_containing_value_objects(*attributes, model_class:) ⇒ Object
- .set_attribute_map_registry(registry) ⇒ Object
Instance Method Summary collapse
- #create_collection(models:, optional_attributes: nil) ⇒ Object
- #create_errors(model:) ⇒ Object
- #create_object(model:, optional_attributes: nil) ⇒ Object
Class Attribute Details
.attribute_map_registry ⇒ Object (readonly)
Returns the value of attribute attribute_map_registry.
6 7 8 |
# File 'lib/pack_api/mapping/value_object_factory.rb', line 6 def attribute_map_registry @attribute_map_registry end |
.value_object_attributes ⇒ Object (readonly)
Returns the value of attribute value_object_attributes.
6 7 8 |
# File 'lib/pack_api/mapping/value_object_factory.rb', line 6 def value_object_attributes @value_object_attributes end |
Class Method Details
.model_attributes_containing_value_objects(*attributes, model_class:) ⇒ Object
13 14 15 16 |
# File 'lib/pack_api/mapping/value_object_factory.rb', line 13 def model_attributes_containing_value_objects(*attributes, model_class:) @value_object_attributes ||= {} @value_object_attributes[model_class] = attributes end |
.set_attribute_map_registry(registry) ⇒ Object
8 9 10 11 |
# File 'lib/pack_api/mapping/value_object_factory.rb', line 8 def set_attribute_map_registry(registry) @attribute_map_registry = registry @value_object_attributes ||= {} end |
Instance Method Details
#create_collection(models:, optional_attributes: nil) ⇒ Object
31 32 33 34 35 |
# File 'lib/pack_api/mapping/value_object_factory.rb', line 31 def create_collection(models:, optional_attributes: nil) return [] if models.blank? models.filter_map { |model| create_object(model:, optional_attributes:) } end |
#create_errors(model:) ⇒ Object
37 38 39 |
# File 'lib/pack_api/mapping/value_object_factory.rb', line 37 def create_errors(model:) attribute_map(model.class, model.errors).attributes end |
#create_object(model:, optional_attributes: nil) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/pack_api/mapping/value_object_factory.rb', line 19 def create_object(model:, optional_attributes: nil) return nil if model.blank? = .compute_if_absent(optional_attributes) { { optional_attributes: } } attribute_map = attribute_map(model.class, model, ) attribute_map.api_type.new(attribute_map.attributes) rescue Dry::Struct::Error => e model_id = model.respond_to?(:id) ? "(id #{model.id})" : '' raise PackAPI::InternalError, "Unable to convert #{model.class.name} #{model_id} to value object (#{e.})" end |