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.



106
107
108
# File 'lib/serega/plugins/batch/batch.rb', line 106

def initialize(opts)
  @opts = opts
end

Instance Attribute Details

#optsObject (readonly)

Returns the value of attribute opts.



104
105
106
# File 'lib/serega/plugins/batch/batch.rb', line 104

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/



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/serega/plugins/batch/batch.rb', line 119

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



138
139
140
# File 'lib/serega/plugins/batch/batch.rb', line 138

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