module Cuprum::Collections::Commands::AbstractFindMany

Abstract implementation of the FindMany command.

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

Private Instance Methods

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

  (scope || build_query).where { { key => one_of(primary_keys) } }
end
handle_missing_items(allow_partial:, items:, primary_keys:) click to toggle source
# File lib/cuprum/collections/commands/abstract_find_many.rb, line 20
def handle_missing_items(allow_partial:, items:, primary_keys:)
  found, missing = match_items(items: items, primary_keys: primary_keys)

  return found if missing.empty?

  return found if allow_partial && !found.empty?

  error = Cuprum::Collections::Errors::NotFound.new(
    collection_name:    collection_name,
    primary_key_name:   primary_key_name,
    primary_key_values: missing
  )
  Cuprum::Result.new(error: error)
end
items_with_primary_keys(items:) click to toggle source
# File lib/cuprum/collections/commands/abstract_find_many.rb, line 35
def items_with_primary_keys(items:)
  # :nocov:
  items.map { |item| [item.send(primary_key_name), item] }.to_h
  # :nocov:
end
match_items(items:, primary_keys:) click to toggle source
# File lib/cuprum/collections/commands/abstract_find_many.rb, line 41
def match_items(items:, primary_keys:)
  items   = items_with_primary_keys(items: items)
  found   = []
  missing = []

  primary_keys.each do |key|
    item = items[key]

    item.nil? ? (missing << key) : (found << item)
  end

  [found, missing]
end
process( primary_keys:, allow_partial: false, envelope: false, scope: nil ) click to toggle source
# File lib/cuprum/collections/commands/abstract_find_many.rb, line 55
def process(
  primary_keys:,
  allow_partial: false,
  envelope:      false,
  scope:         nil
)
  query = apply_query(primary_keys: primary_keys, scope: scope)
  items = step do
    handle_missing_items(
      allow_partial: allow_partial,
      items:         query.to_a,
      primary_keys:  primary_keys
    )
  end

  envelope ? wrap_items(items) : items
end
wrap_items(items) click to toggle source
# File lib/cuprum/collections/commands/abstract_find_many.rb, line 73
def wrap_items(items)
  { collection_name => items }
end