class Cuprum::Collections::Basic::Commands::InsertOne

Command for inserting an entity into the collection.

Private Instance Methods

find_existing(entity:) click to toggle source
# File lib/cuprum/collections/basic/commands/insert_one.rb, line 28
def find_existing(entity:)
  value = entity[primary_key_name.to_s]
  index = data.index { |item| item[primary_key_name.to_s] == value }

  return if index.nil?

  error = Cuprum::Collections::Errors::AlreadyExists.new(
    collection_name:    collection_name,
    primary_key_name:   primary_key_name,
    primary_key_values: value
  )
  failure(error)
end
process(entity:) click to toggle source
# File lib/cuprum/collections/basic/commands/insert_one.rb, line 42
def process(entity:)
  step { find_existing(entity: entity) }

  data << tools.hash_tools.deep_dup(entity)

  entity
end