Class: Cuprum::Collections::Adapter
- Inherits:
-
Object
- Object
- Cuprum::Collections::Adapter
- Includes:
- ResultHelpers, Steps
- Defined in:
- lib/cuprum/collections/adapter.rb
Overview
Utility class for converting between raw attributes and a data format.
Direct Known Subclasses
Cuprum::Collections::Adapters::DataAdapter, Cuprum::Collections::Adapters::EntityAdapter, Cuprum::Collections::Adapters::HashAdapter
Instance Attribute Summary collapse
-
#attribute_names ⇒ Set<String>
readonly
The valid attribute names for a data object.
-
#default_contract ⇒ Stannum::Constraints:Base
readonly
The contract used to validate instances of the data object.
-
#entity_class ⇒ Class
readonly
The class of the data objects.
Instance Method Summary collapse
-
#allow_extra_attributes? ⇒ true, false
If false, attributes methods return an error for attributes not in attributes_names.
-
#build(attributes:) ⇒ Cuprum::Result<Object>
Generates a data object from an attributes hash.
-
#initialize(**options) ⇒ Adapter
constructor
A new instance of Adapter.
-
#merge(attributes:, entity:) ⇒ Cuprum::Result<Object>
Returns a data object with updated attributes.
-
#serialize(entity:) ⇒ Cuprum::Result<Object>
Generates an attributes hash from a data object.
-
#validate(entity:, contract: nil) ⇒ Cuprum::Result<Object>
Validates a data object.
-
#validate_attributes_parameter(attributes, as: 'attributes') ⇒ String?
Asserts that an attributes parameter is a Hash with valid keys.
-
#validate_entity_parameter(entity, as: 'entity') ⇒ String?
Asserts that an entity parameter is of valid type.
Constructor Details
#initialize(**options) ⇒ Adapter
Returns a new instance of Adapter.
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/cuprum/collections/adapter.rb', line 24 def initialize(**) # rubocop:disable Metrics/MethodLength @attribute_names = .fetch(:attribute_names, []) .compact .map(&:to_s) .then { |ary| Set.new(ary) } @default_contract = [:default_contract] @entity_class = [:entity_class] @allow_extra_attributes = .fetch(:allow_extra_attributes, @attribute_names.empty?) validate_entity_class_parameter(@entity_class) end |
Instance Attribute Details
#attribute_names ⇒ Set<String> (readonly)
Returns the valid attribute names for a data object.
40 41 42 |
# File 'lib/cuprum/collections/adapter.rb', line 40 def attribute_names @attribute_names end |
#default_contract ⇒ Stannum::Constraints:Base (readonly)
Returns the contract used to validate instances of the data object.
44 45 46 |
# File 'lib/cuprum/collections/adapter.rb', line 44 def default_contract @default_contract end |
#entity_class ⇒ Class (readonly)
Returns the class of the data objects.
47 48 49 |
# File 'lib/cuprum/collections/adapter.rb', line 47 def entity_class @entity_class end |
Instance Method Details
#allow_extra_attributes? ⇒ true, false
Returns if false, attributes methods return an error for attributes not in attributes_names.
51 52 53 |
# File 'lib/cuprum/collections/adapter.rb', line 51 def allow_extra_attributes? @allow_extra_attributes end |
#build(attributes:) ⇒ Cuprum::Result<Object>
Generates a data object from an attributes hash.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/cuprum/collections/adapter.rb', line 61 def build(attributes:) steps do handle_invalid_parameters( *validate_attributes_parameter(attributes) ) handle_extra_attributes(attributes) build_entity(attributes:) end end |
#merge(attributes:, entity:) ⇒ Cuprum::Result<Object>
Returns a data object with updated attributes.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/cuprum/collections/adapter.rb', line 79 def merge(attributes:, entity:) steps do handle_invalid_parameters( *validate_attributes_parameter(attributes), validate_entity_parameter(entity) ) handle_extra_attributes(attributes) merge_entity(attributes:, entity:) end end |
#serialize(entity:) ⇒ Cuprum::Result<Object>
Generates an attributes hash from a data object.
97 98 99 100 101 102 103 104 105 |
# File 'lib/cuprum/collections/adapter.rb', line 97 def serialize(entity:) steps do handle_invalid_parameters( validate_entity_parameter(entity) ) serialize_entity(entity:) end end |
#validate(entity:, contract: nil) ⇒ Cuprum::Result<Object>
Validates a data object.
115 116 117 118 119 120 121 122 123 |
# File 'lib/cuprum/collections/adapter.rb', line 115 def validate(entity:, contract: nil) steps do handle_invalid_parameters( validate_entity_parameter(entity) ) validate_entity(contract:, entity:) end end |
#validate_attributes_parameter(attributes, as: 'attributes') ⇒ String?
Asserts that an attributes parameter is a Hash with valid keys.
133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/cuprum/collections/adapter.rb', line 133 def validate_attributes_parameter(attributes, as: 'attributes') return attributes_not_hash_error(as:) unless attributes.is_a?(Hash) attributes .each_key .with_object([]) do |key, | << validate_attributes_parameter_key( key, as: "#{as}[#{key.inspect}] key" ) end .compact end |
#validate_entity_parameter(entity, as: 'entity') ⇒ String?
Asserts that an entity parameter is of valid type.
154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/cuprum/collections/adapter.rb', line 154 def validate_entity_parameter(entity, as: 'entity') return unless entity_class return if entity.is_a?(entity_class) tools.assertions.( 'sleeping_king_studios.tools.assertions.instance_of', as:, expected: entity_class ) end |