class Cuprum::Collections::Basic::Commands::UpdateOne

Command for updating an entity in the collection.

Private Instance Methods

find_existing(entity:) click to toggle source
# File lib/cuprum/collections/basic/commands/update_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 index unless index.nil?

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

  entity = data[index].merge(entity)

  data[index] = entity.dup

  entity
end