Class: Cuprum::Rails::Commands::AssignOne

Inherits:
Cuprum::Rails::Command show all
Defined in:
lib/cuprum/rails/commands/assign_one.rb

Overview

Command for assigning attributes to an ActiveRecord model.

Instance Attribute Summary

Attributes inherited from Cuprum::Rails::Command

#collection_name, #member_name, #options, #record_class

Instance Method Summary collapse

Methods inherited from Cuprum::Rails::Command

#initialize, #primary_key_name, #primary_key_type, subclass

Constructor Details

This class inherits a constructor from Cuprum::Rails::Command

Instance Method Details

#call(attributes: , entity: ) ⇒ ActiveRecord::Base

Assigns the given attributes to the record.

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

Examples:

Assigning attributes

entity = Book.new(
  'title'    => 'The Hobbit',
  'author'   => 'J.R.R. Tolkien',
  'series'   => nil,
  'category' => 'Science Fiction and Fantasy'
)
attributes = { title: 'The Silmarillion' }
command    = Assign.new(record_class: Book)
result     = command.call(attributes: attributes, entity: entity)
result.value.attributes
#=> {
  'id'       => nil,
  '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 (ActiveRecord::Base) (defaults to: )

    The record to update.

Returns:

  • (ActiveRecord::Base)

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



43
44
45
46
47
# File 'lib/cuprum/rails/commands/assign_one.rb', line 43

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