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



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

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

Instance Attribute Details

#loadersObject (readonly)

Returns the value of attribute loaders.



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

def loaders
  @loaders
end

#manyObject (readonly)

Returns the value of attribute many.



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

def many
  @many
end

#optsObject (readonly)

Returns the value of attribute opts.



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

def opts
  @opts
end

Instance Method Details

#default_valueObject

Returns default value for missing key.

Returns:

  • (Object)

    default value for missing key



180
181
182
183
184
185
186
# File 'lib/serega/plugins/batch/batch.rb', line 180

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

#keyObject

Returns key (uid) of batch loaded object.

Returns:

  • (Object)

    key (uid) of batch loaded object



172
173
174
175
176
177
# File 'lib/serega/plugins/batch/batch.rb', line 172

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

#loader#call

Returns batch loader.

Returns:

  • (#call)

    batch loader



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

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