Class: Serega::SeregaPlugins::Batch::BatchOptionModel

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

Overview

Combines options and methods needed to load batch for specific attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute) ⇒ void

Initializes BatchOptionModel

Parameters:

  • map_point (Serega::SeregaMapPoint)

    Map point for attribute with :batch option

  • loaders (Array)

    Array of all loaders defined in serialize class

  • many (Boolean)

    Option :many, defined on attribute



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

def initialize(attribute)
  @attribute = attribute
  @opts = attribute.batch
  @loaders = attribute.class.serializer_class.config.batch_loaders
  @many = attribute.many
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



10
11
12
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 10

def attribute
  @attribute
end

#loadersObject (readonly)

Returns the value of attribute loaders.



10
11
12
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 10

def loaders
  @loaders
end

#manyObject (readonly)

Returns the value of attribute many.



10
11
12
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 10

def many
  @many
end

#optsObject (readonly)

Returns the value of attribute opts.



10
11
12
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 10

def opts
  @opts
end

Instance Method Details

#default_valueObject

Returns default value to use if batch loader does not return value for some key

Returns:

  • (Object)

    default value for missing key



55
56
57
58
59
60
61
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 55

def default_value
  if opts.key?(:default)
    opts[:default]
  elsif many
    FROZEN_EMPTY_ARRAY
  end
end

#keyObject

Returns proc that will be used to find batch_key for current attribute.

Returns:

  • (Object)

    key (uid) of batch loaded object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 39

def key
  @batch_key ||= begin
    key = opts[:key]

    if key.is_a?(Symbol)
      proc do |object|
        handle_no_method_error { object.public_send(key) }
      end
    else
      key
    end
  end
end

#loader#call

Returns proc that will be used to batch load registered keys values

Returns:

  • (#call)

    batch loader



29
30
31
32
33
34
35
# File 'lib/serega/plugins/batch/lib/batch_option_model.rb', line 29

def loader
  @batch_loader ||= begin
    loader = opts[:loader]
    loader = loaders.fetch(loader) if loader.is_a?(Symbol)
    loader
  end
end