Module: Knuckles::Stages::Hydrator

Extended by:
Hydrator
Included in:
Hydrator
Defined in:
lib/knuckles/stages/hydrator.rb

Overview

The hydrator converts minimal objects in a prepared collection into fully “hydrated” versions of the same record. For example, the initial ‘model` may only have the `id` and `updated_at` timestamp selected, which is ideal for fetching from the cache. If the object wasn’t in the cache then all of the fields are needed for a complete rendering, so the hydration call will use the passed relation to fetch the full model and any associations.

This is a generic hydrator suitable for any type of collection. If you are working with ‘ActiveRecord` you’ll want to use the ‘Knuckles::Active::Hydrator` module instead.

Instance Method Summary collapse

Instance Method Details

#call(prepared, options) ⇒ Object

Convert all uncached objects into their full representation.

Examples:

Hydrating missing objects


Knuckles::Hydrator.call(
  prepared,
  hydrate: -> (objects) { objects.each(&:fetch!) }
)

Parameters:

  • prepared (Enumerable)

    The prepared collection for processing

  • [Proc, (Hash)

    a customizable set of options



32
33
34
35
36
37
38
39
40
# File 'lib/knuckles/stages/hydrator.rb', line 32

def call(prepared, options)
  hydrate = options[:hydrate]

  if hydrate && any_missing?(prepared)
    hydrate.call(hydratable(prepared))
  end

  prepared
end