Class: Servus::Extensions::Lazily::Resolver Private

Inherits:
Object
  • Object
show all
Defined in:
lib/servus/extensions/lazily/resolver.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Performs the actual record resolution for a lazily-declared input.

Handles the decision logic: is the raw value already an instance, nil, an array, or a lookup value? Delegates to the appropriate finder method on the target class.

Class Method Summary collapse

Class Method Details

.call(raw, klass:, by:, name:) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Resolves a raw value to a record (or collection of records).

Parameters:

  • raw (Object)

    the raw input value (ID, instance, Array, or nil)

  • klass (Class)

    the target model class

  • by (Symbol)

    the lookup column

  • name (Symbol)

    the param name (for error messages)

Returns:

  • (Object)

    the resolved record or collection

Raises:



22
23
24
25
26
27
28
# File 'lib/servus/extensions/lazily/resolver.rb', line 22

def self.call(raw, klass:, by:, name:)
  return raw if raw.is_a?(klass)
  raise Errors::NotFoundError, "Couldn't find #{klass} (#{name} was nil)" if raw.nil?
  return klass.where(by => raw) if raw.is_a?(Array)

  by == :id ? klass.find(raw) : klass.find_by!(by => raw)
end