Module: Servus::Extensions::Lazily::Call
- Defined in:
- lib/servus/extensions/lazily/call.rb
Overview
Instance Method Summary collapse
-
#lazily(name, finds:, by: :id) ⇒ void
Declares a lazy record resolver for a service input.
-
#lazy_resolvers ⇒ Hash{Symbol => Hash}
private
Returns the hash of registered lazy resolvers for this service class.
Instance Method Details
#lazily(name, finds:, by: :id) ⇒ void
Only available when ActiveRecord is loaded (via Railtie)
This method returns an undefined value.
Declares a lazy record resolver for a service input.
Defines an accessor method that lazily resolves the input value to a record. If the value is already an instance of the target class, it is returned directly. If the value is an ID (or other lookup value), it is resolved via the target class’s .find or .find_by! method. Arrays are resolved via .where.
The resolved record is written back to the instance variable, so subsequent calls return the same object without re-querying.
45 46 47 48 |
# File 'lib/servus/extensions/lazily/call.rb', line 45 def lazily(name, finds:, by: :id) (@lazy_resolvers ||= {})[name] = { klass: finds, by: by } define_resolver_method(name, finds, by) end |
#lazy_resolvers ⇒ Hash{Symbol => Hash}
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.
Returns the hash of registered lazy resolvers for this service class.
54 55 56 |
# File 'lib/servus/extensions/lazily/call.rb', line 54 def lazy_resolvers @lazy_resolvers || {} end |