Class: Cuprum::Collections::Commands::Update

Inherits:
Cuprum::Command
  • Object
show all
Defined in:
lib/cuprum/collections/commands/update.rb

Overview

Command for assigning, validating and updating an entity in a collection.

Examples:

Updating An Entity

command =
  Cuprum::Collections::Commands::Create.new(collection:)
  .new(collection: books_collection)
entity  =
  books_collection
  .find_matching { { 'title' => 'Gideon the Ninth' } }
  .value
  .first

# With Invalid Attributes
attributes = { 'author' => '' }
result     = command.call(attributes: attributes)
result.success?
#=> false
result.error
#=> an instance of Cuprum::Collections::Errors::FailedValidation
books_collection
  .find_matching { { 'title' => 'Gideon the Ninth' } }
  .value
  .first['author']
#=> 'Tamsyn Muir'

# With Valid Attributes
attributes = { 'series' => 'The Locked Tomb' }
result     = command.call(attributes: attributes)
result.success?
#=> true
result.value
#=> an instance of Book with title 'Gideon the Ninth' and series
    'The Locked Tomb'
books_collection
  .find_matching { { 'title' => 'Gideon the Ninth' } }
  .value
  .first['series']
#=> 'The Locked Tomb'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection:, contract: nil) ⇒ Update

Returns a new instance of Update.

Parameters:

  • collection (Object)

    The collection used to store the entity.

  • contract (Stannum::Constraint) (defaults to: nil)

    The constraint used to validate the entity. If not given, defaults to the default contract for the collection.



49
50
51
52
53
54
# File 'lib/cuprum/collections/commands/update.rb', line 49

def initialize(collection:, contract: nil)
  super()

  @collection = collection
  @contract   = contract
end

Instance Attribute Details

#collectionObject (readonly)

Returns the collection used to store the entity.

Returns:

  • (Object)

    the collection used to store the entity.



57
58
59
# File 'lib/cuprum/collections/commands/update.rb', line 57

def collection
  @collection
end

#contractStannum::Constraint (readonly)

Returns the constraint used to validate the entity.

Returns:

  • (Stannum::Constraint)

    the constraint used to validate the entity.



60
61
62
# File 'lib/cuprum/collections/commands/update.rb', line 60

def contract
  @contract
end