Class: Serega::SeregaPlugins::Batch::BatchLoadersConfig

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

Overview

Batch loader config

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts) ⇒ BatchLoadersConfig

Returns a new instance of BatchLoadersConfig.



112
113
114
# File 'lib/serega/plugins/batch/batch.rb', line 112

def initialize(opts)
  @opts = opts
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



110
111
112
# File 'lib/serega/plugins/batch/batch.rb', line 110

def opts
  @opts
end

Instance Method Details

#define(loader_name, &block) ⇒ void

This method returns an undefined value.

Defines batch loader

Parameters:

  • loader_name (Symbol)

    Batch loader name, that is used when defining attribute with batch loader.

  • block (Proc)

    Block that can accept 3 parameters - keys, context, map_point and returns hash where ids are keys and values are batch loaded objects/



125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/serega/plugins/batch/batch.rb', line 125

def define(loader_name, &block)
  unless block
    raise SeregaError, "Block must be given to batch_loaders.define method"
  end

  params = block.parameters
  if params.count > 3 || !params.map!(&:first).all? { |type| (type == :req) || (type == :opt) }
    raise SeregaError, "Block can have maximum 3 regular parameters"
  end

  opts[loader_name] = block
end

#fetch(loader_name) ⇒ Proc

Finds previously defined batch loader by name

Parameters:

  • loader_name (Symbol)

Returns:

  • (Proc)

    batch loader block



144
145
146
# File 'lib/serega/plugins/batch/batch.rb', line 144

def fetch(loader_name)
  opts[loader_name] || (raise SeregaError, "Batch loader with name `#{loader_name.inspect}` was not defined. Define example: config.batch_loaders.define(:#{loader_name}) { |keys, ctx, points| ... }")
end