Commit 0b8ce93e authored by Patrick Bajao's avatar Patrick Bajao

Merge branch 'kassio/bulkimports-refactoring' into 'master'

Simplify BulkImport concerns

See merge request gitlab-org/gitlab!47923
parents e1dff736 7740feb3
......@@ -43,7 +43,6 @@ RSpec.describe BulkImports::EE::Groups::Pipelines::EpicsPipeline do
describe 'pipeline parts' do
it { expect(described_class).to include_module(BulkImports::Pipeline) }
it { expect(described_class).to include_module(BulkImports::Pipeline::Attributes) }
it { expect(described_class).to include_module(BulkImports::Pipeline::Runner) }
it 'has extractors' do
......
......@@ -3,10 +3,77 @@
module BulkImports
module Pipeline
extend ActiveSupport::Concern
include Gitlab::ClassAttributes
included do
include Attributes
include Runner
private
def extractors
@extractors ||= self.class.extractors.map(&method(:instantiate))
end
def transformers
@transformers ||= self.class.transformers.map(&method(:instantiate))
end
def loaders
@loaders ||= self.class.loaders.map(&method(:instantiate))
end
def after_run
@after_run ||= self.class.after_run_callback
end
def pipeline_name
@pipeline ||= self.class.name
end
def instantiate(class_config)
class_config[:klass].new(class_config[:options])
end
end
class_methods do
def extractor(klass, options = nil)
add_attribute(:extractors, klass, options)
end
def transformer(klass, options = nil)
add_attribute(:transformers, klass, options)
end
def loader(klass, options = nil)
add_attribute(:loaders, klass, options)
end
def after_run(&block)
class_attributes[:after_run] = block
end
def extractors
class_attributes[:extractors]
end
def transformers
class_attributes[:transformers]
end
def loaders
class_attributes[:loaders]
end
def after_run_callback
class_attributes[:after_run]
end
private
def add_attribute(sym, klass, options)
class_attributes[sym] ||= []
class_attributes[sym] << { klass: klass, options: options }
end
end
end
end
# frozen_string_literal: true
module BulkImports
module Pipeline
module Attributes
extend ActiveSupport::Concern
include Gitlab::ClassAttributes
class_methods do
def extractor(klass, options = nil)
add_attribute(:extractors, klass, options)
end
def transformer(klass, options = nil)
add_attribute(:transformers, klass, options)
end
def loader(klass, options = nil)
add_attribute(:loaders, klass, options)
end
def after_run(&block)
class_attributes[:after_run] = block
end
def add_attribute(sym, klass, options)
class_attributes[sym] ||= []
class_attributes[sym] << { klass: klass, options: options }
end
def extractors
class_attributes[:extractors]
end
def transformers
class_attributes[:transformers]
end
def loaders
class_attributes[:loaders]
end
def after_run_callback
class_attributes[:after_run]
end
end
end
end
end
......@@ -5,34 +5,6 @@ module BulkImports
module Runner
extend ActiveSupport::Concern
included do
private
def extractors
@extractors ||= self.class.extractors.map(&method(:instantiate))
end
def transformers
@transformers ||= self.class.transformers.map(&method(:instantiate))
end
def loaders
@loaders ||= self.class.loaders.map(&method(:instantiate))
end
def after_run
@after_run ||= self.class.after_run_callback
end
def pipeline_name
@pipeline ||= self.class.name
end
def instantiate(class_config)
class_config[:klass].new(class_config[:options])
end
end
def run(context)
info(context, message: "Pipeline started", pipeline: pipeline_name)
......
......@@ -72,7 +72,6 @@ RSpec.describe BulkImports::Groups::Pipelines::GroupPipeline do
describe 'pipeline parts' do
it { expect(described_class).to include_module(BulkImports::Pipeline) }
it { expect(described_class).to include_module(BulkImports::Pipeline::Attributes) }
it { expect(described_class).to include_module(BulkImports::Pipeline::Runner) }
it 'has extractors' do
......
......@@ -55,7 +55,6 @@ RSpec.describe BulkImports::Groups::Pipelines::SubgroupEntitiesPipeline do
describe 'pipeline parts' do
it { expect(described_class).to include_module(BulkImports::Pipeline) }
it { expect(described_class).to include_module(BulkImports::Pipeline::Attributes) }
it { expect(described_class).to include_module(BulkImports::Pipeline::Runner) }
it 'has extractors' do
......
......@@ -2,7 +2,7 @@
require 'spec_helper'
RSpec.describe BulkImports::Pipeline::Attributes do
RSpec.describe BulkImports::Pipeline do
describe 'pipeline attributes' do
before do
stub_const('BulkImports::Extractor', Class.new)
......@@ -10,7 +10,7 @@ RSpec.describe BulkImports::Pipeline::Attributes do
stub_const('BulkImports::Loader', Class.new)
klass = Class.new do
include BulkImports::Pipeline::Attributes
include BulkImports::Pipeline
extractor BulkImports::Extractor, { foo: :bar }
transformer BulkImports::Transformer, { foo: :bar }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment