Class: Cuprum::Collections::Commands::Create

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

Overview

Command for building, validating and inserting an entity into a collection.

Examples:

Creating An Entity

command =
  Cuprum::Collections::Commands::Create.new(collection:)
  .new(collection: books_collection)

# With Invalid Attributes
attributes = { 'title' => '' }
result     = command.call(attributes: attributes)
result.success?
#=> false
result.error
#=> an instance of Cuprum::Collections::Errors::FailedValidation
books_collection.query.count
#=> 0

# With Valid Attributes
attributes = { 'title' => 'Gideon the Ninth' }
result     = command.call(attributes: attributes)
result.success?
#=> true
result.value
#=> a Book with title 'Gideon the Ninth'
books_collection.query.count
#=> 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Create.

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.



37
38
39
40
41
42
# File 'lib/cuprum/collections/commands/create.rb', line 37

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.



45
46
47
# File 'lib/cuprum/collections/commands/create.rb', line 45

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.



48
49
50
# File 'lib/cuprum/collections/commands/create.rb', line 48

def contract
  @contract
end