Class: Gitlab::Ci::Config::Extendable::Entry

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/ci/config/extendable/entry.rb

Constant Summary collapse

InvalidExtensionError =
Class.new(Extendable::ExtensionError)
CircularDependencyError =
Class.new(Extendable::ExtensionError)
NestingTooDeepError =
Class.new(Extendable::ExtensionError)
MAX_NESTING_LEVELS =
10

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(key, context, parent = nil) ⇒ Entry

Returns a new instance of Entry.


18
19
20
21
22
23
24
25
26
# File 'lib/gitlab/ci/config/extendable/entry.rb', line 18

def initialize(key, context, parent = nil)
  @key = key
  @context = context
  @parent = parent

  unless @context.key?(@key)
    raise StandardError, 'Invalid entry key!'
  end
end

Instance Attribute Details

#keyObject (readonly)

Returns the value of attribute key


16
17
18
# File 'lib/gitlab/ci/config/extendable/entry.rb', line 16

def key
  @key
end

Instance Method Details

#ancestorsObject


56
57
58
59
60
# File 'lib/gitlab/ci/config/extendable/entry.rb', line 56

def ancestors
  strong_memoize(:ancestors) do
    Array(@parent&.ancestors) + Array(@parent&.key)
  end
end

#base_hashes!Object


38
39
40
41
42
43
44
45
46
# File 'lib/gitlab/ci/config/extendable/entry.rb', line 38

def base_hashes!
  strong_memoize(:base_hashes) do
    extends_keys.map do |key|
      Extendable::Entry
        .new(key, @context, self)
        .extend!
    end
  end
end

#extend!Object


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
# File 'lib/gitlab/ci/config/extendable/entry.rb', line 62

def extend!
  return value unless extensible?

  if unknown_extensions.any?
    raise Entry::InvalidExtensionError,
          "#{key}: unknown keys in `extends` (#{show_keys(unknown_extensions)})"
  end

  if invalid_bases.any?
    raise Entry::InvalidExtensionError,
          "#{key}: invalid base hashes in `extends` (#{show_keys(invalid_bases)})"
  end

  if nesting_too_deep?
    raise Entry::NestingTooDeepError,
          "#{key}: nesting too deep in `extends`"
  end

  if circular_dependency?
    raise Entry::CircularDependencyError,
          "#{key}: circular dependency detected in `extends`"
  end

  merged = {}
  base_hashes!.each { |h| merged.deep_merge!(h) }

  @context[key] = merged.deep_merge!(value)
end

#extends_keysObject


48
49
50
51
52
53
54
# File 'lib/gitlab/ci/config/extendable/entry.rb', line 48

def extends_keys
  strong_memoize(:extends_keys) do
    next unless extensible?

    Array(value.fetch(:extends)).map(&:to_s).map(&:to_sym)
  end
end

#extensible?Boolean

Returns:

  • (Boolean)

28
29
30
# File 'lib/gitlab/ci/config/extendable/entry.rb', line 28

def extensible?
  value.is_a?(Hash) && value.key?(:extends)
end

#valueObject


32
33
34
35
36
# File 'lib/gitlab/ci/config/extendable/entry.rb', line 32

def value
  strong_memoize(:value) do
    @context.fetch(@key)
  end
end