Module: Serega::SeregaBatch::AttributeLoader::InstanceMethods

Included in:
Serega::SeregaBatch::AttributeLoader
Defined in:
lib/serega/batch/attribute_loader.rb

Overview

Instance methods for AttributeLoader

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#levelObject (readonly)

Returns the value of attribute level.



67
68
69
# File 'lib/serega/batch/attribute_loader.rb', line 67

def level
  @level
end

#pointObject (readonly)

Returns the value of attribute point.



66
67
68
# File 'lib/serega/batch/attribute_loader.rb', line 66

def point
  @point
end

Instance Method Details

#attach(batches) ⇒ void

This method returns an undefined value.

Attaches already loaded batch values to every stored object.

Parameters:

  • batches (Hash)

    Loaded values grouped by batch loader name



60
61
62
63
64
# File 'lib/serega/batch/attribute_loader.rb', line 60

def attach(batches)
  serialized_object_attachers.each do |object, attacher|
    attacher.call(object, batches)
  end
end

#initialize(point, level) ⇒ Object

Parameters:



14
15
16
17
18
# File 'lib/serega/batch/attribute_loader.rb', line 14

def initialize(point, level)
  @point = point
  @level = level
  @serialized_object_attachers = []
end

#load_batch(loader) ⇒ Object

Loads a single named batch for this level's objects. Caching across attributes that reuse the loader is handled by the Level.

Parameters:

Returns:

  • (Object)

    Loaded batch values



51
52
53
54
55
# File 'lib/serega/batch/attribute_loader.rb', line 51

def load_batch(loader)
  level.load(loader)
rescue => error
  reraise_with_serialized_attribute_details(error)
end

#preload_associationsvoid

This method returns an undefined value.

Preloads this attribute's associations onto the level's objects, once for the attribute (not per named batch loader), via the serializer's registered preload_with handler. Raises when :preload is declared but no handler exists, so a declared preload never silently does nothing.



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/serega/batch/attribute_loader.rb', line 33

def preload_associations
  preloads = point.attribute.preloads
  return unless preloads

  handler = point.class.serializer_class.preload_with
  unless handler
    raise SeregaError, "The :preload option requires a preload handler. Register one with `preload_with` (the :activerecord_preloads plugin does this for you)."
  end

  handler.call(level.objects, preloads)
rescue => error
  reraise_with_serialized_attribute_details(error)
end

#store(object, attacher) ⇒ void

This method returns an undefined value.

Stores object with attacher to find and attach attribute values in batch later.

Parameters:

  • object (Object)

    Serialized object

  • attacher (Proc)

    Proc that attaches found values to originated attributes



24
25
26
# File 'lib/serega/batch/attribute_loader.rb', line 24

def store(object, attacher)
  serialized_object_attachers << [object, attacher]
end