2020-10-23 11:08:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module BulkImports
|
|
|
|
module Pipeline
|
|
|
|
extend ActiveSupport::Concern
|
2020-11-20 04:09:06 -05:00
|
|
|
include Gitlab::ClassAttributes
|
2021-02-12 13:08:59 -05:00
|
|
|
include Runner
|
2020-10-23 11:08:42 -04:00
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
def initialize(context)
|
|
|
|
@context = context
|
|
|
|
end
|
2020-11-20 04:09:06 -05:00
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
included do
|
2020-11-20 04:09:06 -05:00
|
|
|
private
|
|
|
|
|
2021-02-12 13:08:59 -05:00
|
|
|
attr_reader :context
|
|
|
|
|
2021-01-12 01:10:31 -05:00
|
|
|
def extractor
|
|
|
|
@extractor ||= instantiate(self.class.get_extractor)
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def transformers
|
|
|
|
@transformers ||= self.class.transformers.map(&method(:instantiate))
|
|
|
|
end
|
|
|
|
|
2021-01-12 01:10:31 -05:00
|
|
|
def loader
|
|
|
|
@loaders ||= instantiate(self.class.get_loader)
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
2020-11-30 10:09:21 -05:00
|
|
|
def pipeline
|
2020-11-20 04:09:06 -05:00
|
|
|
@pipeline ||= self.class.name
|
|
|
|
end
|
|
|
|
|
|
|
|
def instantiate(class_config)
|
|
|
|
class_config[:klass].new(class_config[:options])
|
|
|
|
end
|
2020-11-30 10:09:21 -05:00
|
|
|
|
|
|
|
def abort_on_failure?
|
|
|
|
self.class.abort_on_failure?
|
|
|
|
end
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class_methods do
|
|
|
|
def extractor(klass, options = nil)
|
2021-01-12 01:10:31 -05:00
|
|
|
class_attributes[:extractor] = { klass: klass, options: options }
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def transformer(klass, options = nil)
|
|
|
|
add_attribute(:transformers, klass, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
def loader(klass, options = nil)
|
2021-01-12 01:10:31 -05:00
|
|
|
class_attributes[:loader] = { klass: klass, options: options }
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
2021-01-12 01:10:31 -05:00
|
|
|
def get_extractor
|
|
|
|
class_attributes[:extractor]
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def transformers
|
|
|
|
class_attributes[:transformers]
|
|
|
|
end
|
|
|
|
|
2021-01-12 01:10:31 -05:00
|
|
|
def get_loader
|
|
|
|
class_attributes[:loader]
|
2020-11-20 04:09:06 -05:00
|
|
|
end
|
|
|
|
|
2020-11-30 10:09:21 -05:00
|
|
|
def abort_on_failure!
|
|
|
|
class_attributes[:abort_on_failure] = true
|
|
|
|
end
|
|
|
|
|
|
|
|
def abort_on_failure?
|
|
|
|
class_attributes[:abort_on_failure]
|
|
|
|
end
|
|
|
|
|
2020-11-20 04:09:06 -05:00
|
|
|
private
|
|
|
|
|
|
|
|
def add_attribute(sym, klass, options)
|
|
|
|
class_attributes[sym] ||= []
|
|
|
|
class_attributes[sym] << { klass: klass, options: options }
|
|
|
|
end
|
2020-10-23 11:08:42 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|