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/plugins_extensions.rb,
lib/serega/plugins/batch/lib/validations/check_opt_batch.rb,
lib/serega/plugins/batch/lib/validations/check_batch_opt_key.rb,
lib/serega/plugins/batch/lib/validations/check_batch_opt_loader.rb

Overview

Plugin that can be used to load attributes values in batches. Each batch loader accepts list of selected keys, context and current attribute point. Each batch loader must return Hash with values grouped by provided keys. There are specific :default option that can be used to add default value for missing key.

Examples:

class PostSerializer < Serega
  plugin :batch

  # Define batch loader via callable class, it must accept three args (keys, context, map_point)
  attribute :comments_count, batch: { key: :id, loader: PostCommentsCountBatchLoader, default: 0}

  # Define batch loader via Symbol, later we should define this loader via config.batch_loaders.define(:posts_comments_counter) { ... }
  attribute :comments_count, batch: { key: :id, loader: :posts_comments_counter, default: 0}

  # Define batch loader with serializer
  attribute :comments, serializer: CommentSerializer, batch: { key: :id, loader: :posts_comments, default: []}

  # Resulted block must return hash like { key => value(s) }
  config.batch_loaders.define(:posts_comments_counter) do |keys|
    Comment.group(:post_id).where(post_id: keys).count
  end

  # We can return objects that will be automatically serialized if attribute defined with :serializer
  # Parameter `context` can be used when loading batch
  # Parameter `map_point` can be used to find nested attributes that will be serialized (`map_point.preloads`)
  config.batch_loaders.define(:posts_comments) do |keys, context, map_point|
    Comment.where(post_id: keys).where(is_spam: false).group_by(&:post_id)
  end
end

Defined Under Namespace

Modules: AttributeInstanceMethods, CheckAttributeParamsInstanceMethods, ClassMethods, ConfigInstanceMethods, InstanceMethods, MapPointInstanceMethods, PluginsExtensions, SeregaObjectSerializerInstanceMethods Classes: BatchLoadersConfig, BatchModel, CheckBatchOptKey, 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



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

def self.after_load_plugin(serializer_class, **opts)
  config = serializer_class.config
  config.attribute_keys << :batch
  config.opts[:batch] = {loaders: {}}
  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"
    serializer_class::SeregaBatchLoader.include(PluginsExtensions::ActiveRecordPreloads::BatchLoaderInstanceMethods)
  end

  if serializer_class.plugin_used?(:formatters)
    require_relative "./lib/plugins_extensions"
    serializer_class::SeregaBatchLoader.include(PluginsExtensions::Formatters::BatchLoaderInstanceMethods)
  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)

    Loaded plugins options



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/serega/plugins/batch/batch.rb', line 52

def self.load_plugin(serializer_class, **_opts)
  require_relative "./lib/loader"
  require_relative "./lib/loaders"
  require_relative "./lib/validations/check_batch_opt_key"
  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::SeregaMapPoint.include(MapPointInstanceMethods)
  serializer_class::SeregaObjectSerializer.include(SeregaObjectSerializerInstanceMethods)
end

.plugin_nameSymbol

Returns plugin name

Returns:

  • (Symbol)

    Plugin name



40
41
42
# File 'lib/serega/plugins/batch/batch.rb', line 40

def self.plugin_name
  :batch
end