Class: Cuprum::Collections::Basic::Commands::AssignOne

Inherits:
Cuprum::Collections::Basic::Command show all
Defined in:
lib/cuprum/collections/basic/commands/assign_one.rb

Overview

Command for assigning attributes to a collection entity.

Instance Attribute Summary

Attributes inherited from Cuprum::Collections::Basic::Command

#collection_name, #data, #default_contract, #member_name, #options, #primary_key_name, #primary_key_type

Instance Method Summary collapse

Methods inherited from Cuprum::Collections::Basic::Command

#initialize, subclass

Constructor Details

This class inherits a constructor from Cuprum::Collections::Basic::Command

Instance Method Details

#call(attributes: , entity: ) ⇒ Hash

Assigns the given attributes to the entity.

Any attributes on the entity that are not part of the given attributes hash are unchanged.

Examples:

Assigning attributes

entity = {
  'title'    => 'The Hobbit',
  'author'   => 'J.R.R. Tolkien',
  'series'   => nil,
  'category' => 'Science Fiction and Fantasy'
}
attributes = { title: 'The Silmarillion' }
command = AssignOne.new(collection_name: 'books', data: books)
command.call(attributes: attributes, entity: entity)
#=> {
  'title'    => 'The Silmarillion',
  'author'   => 'J.R.R. Tolkien',
  'series'   => nil,
  'category' => 'Science Fiction and Fantasy'
}

Parameters:

  • attributes (Hash) (defaults to: )

    The attributes and values to update.

  • entity (Hash) (defaults to: )

    The collection entity to update.

Returns:

  • (Hash)

    a copy of the entity, merged with the given attributes.



39
40
41
42
43
44
# File 'lib/cuprum/collections/basic/commands/assign_one.rb', line 39

validate_parameters :call do
  keyword :attributes,
    Stannum::Constraints::Types::HashWithIndifferentKeys.new
  keyword :entity,
    Stannum::Constraints::Types::HashWithStringKeys.new
end