Class: Cuprum::Collections::Commands::Upsert

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

Overview

Command for creating or updating an entity from an attributes Hash.

Examples:

Creating Or Updating An Entity By Primary Key

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

# Creating A New Entity
books_collection.query.count
#=> 0
attributes = {
  'id'     => 0
  'title'  => 'Gideon the Ninth',
  'author' => 'Tamsyn Muir'
}
result = command.call(attributes: attributes)
result.value
#=> a Book with id 0, title 'Gideon the Ninth', and author 'Tamsyn Muir'
books_collection.query.count
#=> 1

# Updating An Existing Entity
attributes = {
  'id'     => 0
  'series' => 'The Locked Tomb'
}
result = command.call(attributes: attributes)
result.value
#=> a Book with id 0, title 'Gideon the Ninth', author 'Tamsyn Muir', and
    series 'The Locked Tomb'
books_collection.query.count
#=> 1

Creating Or Updating An Entity By Attributes

command    =
  Cuprum::Collections::Commands::Upsert
  .new(attribute_names: %w[title], collection: books_collection)

# Creating A New Entity
books_collection.query.count
#=> 0
attributes = {
  'id'     => 0
  'title'  => 'Gideon the Ninth',
  'author' => 'Tamsyn Muir'
}
result = command.call(attributes: attributes)
result.value
#=> a Book with id 0, title 'Gideon the Ninth', and author 'Tamsyn Muir'
books_collection.query.count
#=> 1

# Updating An Existing Entity
attributes = {
  'title'  => 'Gideon the Ninth',
  'series' => 'The Locked Tomb'
}
result = command.call(attributes: attributes)
result.value
#=> a Book with id 0, title 'Gideon the Ninth', author 'Tamsyn Muir', and
    series 'The Locked Tomb'
books_collection.query.count
#=> 1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection:, attribute_names: 'id', contract: nil) ⇒ Upsert

Returns a new instance of Upsert.

Parameters:

  • attribute_names (String, Symbol, Array<String, Symbol>) (defaults to: 'id')

    The names of the attributes used to find the unique entity.

  • 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.



81
82
83
84
85
86
87
# File 'lib/cuprum/collections/commands/upsert.rb', line 81

def initialize(collection:, attribute_names: 'id', contract: nil)
  super()

  @attribute_names = normalize_attribute_names(attribute_names)
  @collection      = collection
  @contract        = contract
end

Instance Attribute Details

#attribute_namesArray<String> (readonly)

Returns the names of the attributes used to find the unique entity.

Returns:

  • (Array<String>)

    the names of the attributes used to find the unique entity.



91
92
93
# File 'lib/cuprum/collections/commands/upsert.rb', line 91

def attribute_names
  @attribute_names
end

#collectionObject (readonly)

Returns the collection used to store the entity.

Returns:

  • (Object)

    the collection used to store the entity.



94
95
96
# File 'lib/cuprum/collections/commands/upsert.rb', line 94

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.



97
98
99
# File 'lib/cuprum/collections/commands/upsert.rb', line 97

def contract
  @contract
end