Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pipeline.rb « bulk_imports « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 06b81b5da14d8ea5962984ad29edd33358460378 (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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# frozen_string_literal: true

module BulkImports
  module Pipeline
    extend ActiveSupport::Concern
    include Gitlab::ClassAttributes

    included do
      include Runner

      private

      def extractor
        @extractor ||= instantiate(self.class.get_extractor)
      end

      def transformers
        @transformers ||= self.class.transformers.map(&method(:instantiate))
      end

      def loader
        @loaders ||= instantiate(self.class.get_loader)
      end

      def after_run
        @after_run ||= self.class.after_run_callback
      end

      def pipeline
        @pipeline ||= self.class.name
      end

      def instantiate(class_config)
        class_config[:klass].new(class_config[:options])
      end

      def abort_on_failure?
        self.class.abort_on_failure?
      end
    end

    class_methods do
      def extractor(klass, options = nil)
        class_attributes[:extractor] = { klass: klass, options: options }
      end

      def transformer(klass, options = nil)
        add_attribute(:transformers, klass, options)
      end

      def loader(klass, options = nil)
        class_attributes[:loader] = { klass: klass, options: options }
      end

      def after_run(&block)
        class_attributes[:after_run] = block
      end

      def get_extractor
        class_attributes[:extractor]
      end

      def transformers
        class_attributes[:transformers]
      end

      def get_loader
        class_attributes[:loader]
      end

      def after_run_callback
        class_attributes[:after_run]
      end

      def abort_on_failure!
        class_attributes[:abort_on_failure] = true
      end

      def abort_on_failure?
        class_attributes[:abort_on_failure]
      end

      private

      def add_attribute(sym, klass, options)
        class_attributes[sym] ||= []
        class_attributes[sym] << { klass: klass, options: options }
      end
    end
  end
end