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)

    Plugin options



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/serega/plugins/batch/batch.rb', line 86

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

.before_load_plugin(serializer_class, **opts) ⇒ void

This method returns an undefined value.

Checks requirements to load plugin

Parameters:

  • serializer_class (Class<Serega>)

    Current serializer class

  • opts (Hash)

    plugin options



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/serega/plugins/batch/batch.rb', line 35

def self.before_load_plugin(serializer_class, **opts)
  allowed_keys = %i[auto_hide id_method]
  opts.each_key do |key|
    next if allowed_keys.include?(key)

    raise SeregaError,
      "Plugin #{plugin_name.inspect} does not accept the #{key.inspect} option. Allowed options:\n" \
      "  - :auto_hide [Boolean] - Marks attribute as hidden when it has :batch loader specified\n" \
      "  - :id_method [Symbol, #call] - Specified the default method to use to find object identifier"
  end
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)

    Plugin options



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/serega/plugins/batch/batch.rb', line 55

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