Class: Cuprum::Collections::Basic::Commands::BuildOne

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

Overview

Command for generating a collection entity from an attributes hash.

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

Builds a new entity with the given attributes.

Examples:

Building an entity

attributes = {
  'title'    => 'The Hobbit',
  'author'   => 'J.R.R. Tolkien',
  'series'   => nil,
  'category' => 'Science Fiction and Fantasy'
}
command    = BuildOne.new(collection_name: 'books', data: books)
result     = command.call(attributes: attributes)
result.value
#=> {
  '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 assign.

Returns:

  • (Hash)

    the newly built entity.



34
35
36
37
# File 'lib/cuprum/collections/basic/commands/build_one.rb', line 34

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