Class: Cuprum::Collections::Adapters::EntityAdapter

Inherits:
Cuprum::Collections::Adapter show all
Defined in:
lib/cuprum/collections/adapters/entity_adapter.rb

Overview

Utility class for converting between attributes and a Stannum::Entity class.

Instance Attribute Summary

Attributes inherited from Cuprum::Collections::Adapter

#attribute_names, #default_contract, #entity_class

Instance Method Summary collapse

Methods inherited from Cuprum::Collections::Adapter

#allow_extra_attributes?, #build, #merge, #serialize, #validate, #validate_attributes_parameter, #validate_entity_parameter

Constructor Details

#initialize(entity_class:, **options) ⇒ EntityAdapter

Returns a new instance of EntityAdapter.

Parameters:

  • options (Hash)

    options for initializing the adapter.

Options Hash (**options):

  • attributes_names (Array<String, Symbol>)

    the valid attribute names for a data object. Defaults to the entity class's attributes. Must be a subset of the entity class's attributes.

  • default_contract (Stannum::Constraints:Base)

    the contract used to validate instances of the data object.

  • entity_class (Class)

    the class of the data objects. Must be a Stannum::Enttiy subclass.



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/cuprum/collections/adapters/entity_adapter.rb', line 18

def initialize(entity_class:, **options)
  if options[:allow_extra_attributes]
    raise ArgumentError, 'adapter does not support extra attributes'
  end

  attribute_names = options.fetch(:attribute_names) do
    entity_class?(entity_class) ? entity_class&.attributes&.keys || [] : []
  end

  super(attribute_names:, entity_class:, **options)

  verify_attribute_names_are_attributes
end