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

prependable.rb « patch « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a9f6cfb19cbf52937a19244e6cb8ea9819a18205 (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
# frozen_string_literal: true

# We're patching `ActiveSupport::Concern` in
# config/initializers/0_as_concern.rb
#
# We want to patch `ActiveSupport::Concern` for two reasons:
# 1. Allow defining class methods via: `class_methods` method
# 2. Allow `prepended do; end` work like `included do; end`
# If we don't need anything above, we don't need this patch nor the concern!

# rubocop:disable Gitlab/ModuleWithInstanceVariables
module Gitlab
  module Patch
    module Prependable
      class MultiplePrependedBlocks < StandardError
        def initialize
          super "Cannot define multiple 'prepended' blocks for a Concern"
        end
      end

      def prepend_features(base)
        return false if prepended?(base)

        super

        if const_defined?(:ClassMethods)
          klass_methods = const_get(:ClassMethods)
          base.singleton_class.prepend klass_methods
          base.instance_variable_set(:@_prepended_class_methods, klass_methods)
        end

        if instance_variable_defined?(:@_prepended_block)
          base.class_eval(&@_prepended_block)
        end

        true
      end

      def class_methods
        super

        if instance_variable_defined?(:@_prepended_class_methods)
          const_get(:ClassMethods).prepend @_prepended_class_methods
        end
      end

      def prepended(base = nil, &block)
        if base.nil?
          raise MultiplePrependedBlocks if
            instance_variable_defined?(:@_prepended_block)

          @_prepended_block = block
        else
          super
        end
      end

      def prepended?(base)
        index = base.ancestors.index(base)

        base.ancestors[0...index].index(self)
      end
    end
  end
end