Class: PackAPI::Mapping::ValueObjectFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/pack_api/mapping/value_object_factory.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.attribute_map_registryObject (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_attributesObject (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?

  options = attribute_map_options_cache.compute_if_absent(optional_attributes) { { optional_attributes: } }
  attribute_map = attribute_map(model.class, model, options)
  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.message})"
end