summaryrefslogtreecommitdiff
path: root/lib/gitlab/batch_worker_context.rb
blob: 9bc877fcd8d4e5daa1055cb04042357328df8e6c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

module Gitlab
  class BatchWorkerContext
    def initialize(objects, arguments_proc:, context_proc:)
      @objects = objects
      @arguments_proc = arguments_proc
      @context_proc = context_proc
    end

    def arguments
      context_by_arguments.keys
    end

    def context_for(arguments)
      context_by_arguments[arguments]
    end

    private

    attr_reader :objects, :arguments_proc, :context_proc

    def context_by_arguments
      @context_by_arguments ||= objects.each_with_object({}) do |object, result|
        arguments = Array.wrap(arguments_proc.call(object))
        context = Gitlab::ApplicationContext.new(**context_proc.call(object))

        result[arguments] = context
      end
    end
  end
end