module Cuprum::Collections::Commands::AbstractFindOne

Abstract implementation of the FindOne command.

Subclasses must define the build_query method, which returns an empty Query instance for that collection.

Private Instance Methods

apply_query(primary_key:, scope:) click to toggle source
# File lib/cuprum/collections/commands/abstract_find_one.rb, line 14
def apply_query(primary_key:, scope:)
  key = primary_key_name

  (scope || build_query).where { { key => equals(primary_key) } }.limit(1)
end
handle_missing_item(item:, primary_key:) click to toggle source
# File lib/cuprum/collections/commands/abstract_find_one.rb, line 20
def handle_missing_item(item:, primary_key:)
  return if item

  error = Cuprum::Collections::Errors::NotFound.new(
    collection_name:    collection_name,
    primary_key_name:   primary_key_name,
    primary_key_values: [primary_key]
  )
  Cuprum::Result.new(error: error)
end
process(envelope:, primary_key:, scope:) click to toggle source
# File lib/cuprum/collections/commands/abstract_find_one.rb, line 31
def process(envelope:, primary_key:, scope:)
  query = apply_query(primary_key: primary_key, scope: scope)
  item  = query.to_a.first

  step { handle_missing_item(item: item, primary_key: primary_key) }

  envelope ? wrap_item(item) : item
end
wrap_item(item) click to toggle source
# File lib/cuprum/collections/commands/abstract_find_one.rb, line 40
def wrap_item(item)
  { member_name => item }
end