Class: Serega::SeregaBatch::Level

Inherits:
Object
  • Object
show all
Defined in:
lib/serega/batch/level.rb

Overview

Objects serialized at one plan level, together with their loaded batch results.

Every attribute serialized at the same level shares one Level, so a named batch loader reused by several of them runs once for the whole set of objects. The same loader over a different level (deeper nesting) uses a different Level and loads again.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Level

Returns a new instance of Level.

Parameters:

  • context (Hash)

    Serialization context



21
22
23
24
25
# File 'lib/serega/batch/level.rb', line 21

def initialize(context)
  @context = context
  @objects = []
  @results = {}.compare_by_identity
end

Instance Attribute Details

#objectsArray (readonly)

Returns Objects gathered for this level.

Returns:

  • (Array)

    Objects gathered for this level



18
19
20
# File 'lib/serega/batch/level.rb', line 18

def objects
  @objects
end

Instance Method Details

#add_objects(objects) ⇒ void

This method returns an undefined value.

Adds a chunk of serialized objects to this level.

Parameters:

  • objects (Array)

    Objects being serialized at this level



30
31
32
# File 'lib/serega/batch/level.rb', line 30

def add_objects(objects)
  @objects.concat(objects)
end

#load(loader) ⇒ Object

Loads and returns values for the given batch loader, once per loader.

Freezes the gathered objects on the first load: all of a level's objects are added before any of its batches load, so sealing the set here guards against a later stray add_objects and against a loader mutating it.

Parameters:

Returns:

  • (Object)

    Loaded values



42
43
44
45
# File 'lib/serega/batch/level.rb', line 42

def load(loader)
  @objects.freeze
  @results[loader] ||= loader.load(@objects, @context)
end