Class: Serega::SeregaPlugins::Batch::BatchModel

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

Overview

Stores batch config for specific attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, loaders, many) ⇒ void

Initializes batch model

Parameters:

  • opts (Hash)

    Attribute :batch option

  • loaders (Array)

    Array of all loaders defined in serialize class

  • many (Boolean)

    Option :many, defined on attribute



157
158
159
160
161
# File 'lib/serega/plugins/batch/batch.rb', line 157

def initialize(opts, loaders, many)
  @opts = opts
  @loaders = loaders
  @many = many
end

Instance Attribute Details

#loadersObject (readonly)

Returns the value of attribute loaders.



147
148
149
# File 'lib/serega/plugins/batch/batch.rb', line 147

def loaders
  @loaders
end

#manyObject (readonly)

Returns the value of attribute many.



147
148
149
# File 'lib/serega/plugins/batch/batch.rb', line 147

def many
  @many
end

#optsObject (readonly)

Returns the value of attribute opts.



147
148
149
# File 'lib/serega/plugins/batch/batch.rb', line 147

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



184
185
186
187
188
189
190
# File 'lib/serega/plugins/batch/batch.rb', line 184

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



175
176
177
178
179
180
# File 'lib/serega/plugins/batch/batch.rb', line 175

def key
  @batch_key ||= begin
    key = opts[:key]
    key.is_a?(Symbol) ? proc { |object| object.public_send(key) } : key
  end
end

#loader#call

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

Returns:

  • (#call)

    batch loader



165
166
167
168
169
170
171
# File 'lib/serega/plugins/batch/batch.rb', line 165

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