Module: Serega::SeregaPlugins::Batch

Defined in:
lib/serega/plugins/batch/batch.rb,
lib/serega/plugins/batch/lib/loader.rb,
lib/serega/plugins/batch/lib/loaders.rb,
lib/serega/plugins/batch/lib/batch_config.rb,
lib/serega/plugins/batch/lib/modules/config.rb,
lib/serega/plugins/batch/lib/modules/attribute.rb,
lib/serega/plugins/batch/lib/modules/plan_point.rb,
lib/serega/plugins/batch/lib/modules/object_serializer.rb,
lib/serega/plugins/batch/lib/plugins_extensions/preloads.rb,
lib/serega/plugins/batch/lib/validations/check_opt_batch.rb,
lib/serega/plugins/batch/lib/modules/attribute_normalizer.rb,
lib/serega/plugins/batch/lib/plugins_extensions/formatters.rb,
lib/serega/plugins/batch/lib/modules/check_attribute_params.rb,
lib/serega/plugins/batch/lib/validations/check_batch_opt_loader.rb,
lib/serega/plugins/batch/lib/validations/check_batch_opt_id_method.rb,
lib/serega/plugins/batch/lib/plugins_extensions/activerecord_preloads.rb

Overview

Plugin ‘:batch`

Must be used to omit N+1 when loading attributes values.

Examples:

Quick example


class AppSerializer
  plugin :batch, id_method: :id
end

class UserSerializer < AppSerializer
  attribute :comments_count, batch: { loader: CommentsCountBatchLoader }, default: 0
  attribute :company, serializer: CompanySerializer, batch: { loader: UserCompanyBatchLoader }
end

Defined Under Namespace

Modules: AttributeInstanceMethods, AttributeNormalizerInstanceMethods, CheckAttributeParamsInstanceMethods, ClassMethods, ConfigInstanceMethods, InstanceMethods, PlanPointInstanceMethods, PluginsExtensions, SeregaObjectSerializerInstanceMethods Classes: BatchConfig, CheckBatchOptIdMethod, CheckBatchOptLoader, CheckOptBatch, SeregaBatchLoader, SeregaBatchLoaders

Class Method Summary collapse

Class Method Details

.after_load_plugin(serializer_class, **opts) ⇒ void

This method returns an undefined value.

Runs callbacks after plugin was attached

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • opts (Hash)

    loaded plugins opts



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/serega/plugins/batch/batch.rb', line 67

def self.after_load_plugin(serializer_class, **opts)
  serializer_class::SeregaConfig.include(ConfigInstanceMethods)

  batch_loaders_class = Class.new(SeregaBatchLoaders)
  batch_loaders_class.serializer_class = serializer_class
  serializer_class.const_set(:SeregaBatchLoaders, batch_loaders_class)

  batch_loader_class = Class.new(SeregaBatchLoader)
  batch_loader_class.serializer_class = serializer_class
  serializer_class.const_set(:SeregaBatchLoader, batch_loader_class)

  if serializer_class.plugin_used?(:activerecord_preloads)
    require_relative "lib/plugins_extensions/activerecord_preloads"
    serializer_class::SeregaBatchLoader.include(PluginsExtensions::ActiveRecordPreloads::BatchLoaderInstanceMethods)
  end

  if serializer_class.plugin_used?(:formatters)
    require_relative "lib/plugins_extensions/formatters"
    serializer_class::SeregaBatchLoader.include(PluginsExtensions::Formatters::BatchLoaderInstanceMethods)
    serializer_class::SeregaAttribute.include(PluginsExtensions::Formatters::SeregaAttributeInstanceMethods)
  end

  if serializer_class.plugin_used?(:preloads)
    require_relative "lib/plugins_extensions/preloads"
    serializer_class::SeregaAttributeNormalizer.include(PluginsExtensions::Preloads::AttributeNormalizerInstanceMethods)
  end

  config = serializer_class.config
  config.attribute_keys << :batch
  config.opts[:batch] = {loaders: {}, id_method: nil, auto_hide: false}
  config.batch.auto_hide = opts[:auto_hide] if opts.key?(:auto_hide)
  config.batch.id_method = opts[:id_method] if opts.key?(:id_method)
end

.load_plugin(serializer_class, **_opts) ⇒ void

This method returns an undefined value.

Applies plugin code to specific serializer

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • _opts (Hash)

    Loaded plugins options



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/serega/plugins/batch/batch.rb', line 36

def self.load_plugin(serializer_class, **_opts)
  require_relative "lib/batch_config"
  require_relative "lib/loader"
  require_relative "lib/loaders"
  require_relative "lib/modules/attribute"
  require_relative "lib/modules/attribute_normalizer"
  require_relative "lib/modules/check_attribute_params"
  require_relative "lib/modules/config"
  require_relative "lib/modules/object_serializer"
  require_relative "lib/modules/plan_point"
  require_relative "lib/validations/check_batch_opt_id_method"
  require_relative "lib/validations/check_batch_opt_loader"
  require_relative "lib/validations/check_opt_batch"

  serializer_class.extend(ClassMethods)
  serializer_class.include(InstanceMethods)
  serializer_class::CheckAttributeParams.include(CheckAttributeParamsInstanceMethods)
  serializer_class::SeregaAttribute.include(AttributeInstanceMethods)
  serializer_class::SeregaAttributeNormalizer.include(AttributeNormalizerInstanceMethods)
  serializer_class::SeregaPlanPoint.include(PlanPointInstanceMethods)
  serializer_class::SeregaObjectSerializer.include(SeregaObjectSerializerInstanceMethods)
end

.plugin_nameSymbol

Returns plugin name

Returns:

  • (Symbol)

    Plugin name



24
25
26
# File 'lib/serega/plugins/batch/batch.rb', line 24

def self.plugin_name
  :batch
end