2020-01-31 04:08:53 -05:00
|
|
|
# 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))
|
2020-12-09 22:09:53 -05:00
|
|
|
context = Gitlab::ApplicationContext.new(**context_proc.call(object))
|
2020-01-31 04:08:53 -05:00
|
|
|
|
|
|
|
result[arguments] = context
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|