Class: Gitlab::Ci::Variables::Collection

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gitlab/ci/variables/collection.rb,
lib/gitlab/ci/variables/collection/item.rb,
lib/gitlab/ci/variables/collection/sort.rb

Defined Under Namespace

Classes: Item, Sort

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(variables = [], errors = nil) ⇒ Collection

Returns a new instance of Collection.


11
12
13
14
15
16
17
# File 'lib/gitlab/ci/variables/collection.rb', line 11

def initialize(variables = [], errors = nil)
  @variables = []
  @variables_by_key = Hash.new { |h, k| h[k] = [] }
  @errors = errors

  variables.each { |variable| self.append(variable) }
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.


9
10
11
# File 'lib/gitlab/ci/variables/collection.rb', line 9

def errors
  @errors
end

Instance Method Details

#+(other) ⇒ Object


41
42
43
44
45
46
# File 'lib/gitlab/ci/variables/collection.rb', line 41

def +(other)
  self.class.new.tap do |collection|
    self.each { |variable| collection.append(variable) }
    other.each { |variable| collection.append(variable) }
  end
end

#[](key) ⇒ Object


48
49
50
# File 'lib/gitlab/ci/variables/collection.rb', line 48

def [](key)
  all(key)&.last
end

#all(key) ⇒ Object


52
53
54
55
# File 'lib/gitlab/ci/variables/collection.rb', line 52

def all(key)
  vars = @variables_by_key[key]
  vars unless vars.empty?
end

#append(resource) ⇒ Object


19
20
21
22
23
24
25
# File 'lib/gitlab/ci/variables/collection.rb', line 19

def append(resource)
  item = Collection::Item.fabricate(resource)
  @variables.append(item)
  @variables_by_key[item[:key]] << item

  self
end

#compactObject


27
28
29
# File 'lib/gitlab/ci/variables/collection.rb', line 27

def compact
  Collection.new(select { |variable| !variable.value.nil? })
end

#concat(resources) ⇒ Object


31
32
33
34
35
# File 'lib/gitlab/ci/variables/collection.rb', line 31

def concat(resources)
  return self if resources.nil?

  tap { resources.each { |variable| self.append(variable) } }
end

#eachObject


37
38
39
# File 'lib/gitlab/ci/variables/collection.rb', line 37

def each
  @variables.each { |variable| yield variable }
end

#expand_value(value, keep_undefined: false) ⇒ Object


75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gitlab/ci/variables/collection.rb', line 75

def expand_value(value, keep_undefined: false)
  value.gsub(Item::VARIABLES_REGEXP) do
    match = Regexp.last_match
    if match[:key]
      # we matched variable
      if variable = self[match[:key]]
        variable.value
      elsif keep_undefined
        match[0]
      end
    else
      # we escape sequence
      match[0]
    end
  end
end

#reject(&block) ⇒ Object


71
72
73
# File 'lib/gitlab/ci/variables/collection.rb', line 71

def reject(&block)
  Collection.new(@variables.reject(&block))
end

#sizeObject


57
58
59
# File 'lib/gitlab/ci/variables/collection.rb', line 57

def size
  @variables.size
end

#sort_and_expand_all(keep_undefined: false) ⇒ Object


92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/gitlab/ci/variables/collection.rb', line 92

def sort_and_expand_all(keep_undefined: false)
  sorted = Sort.new(self)
  return self.class.new(self, sorted.errors) unless sorted.valid?

  new_collection = self.class.new

  sorted.tsort.each do |item|
    unless item.depends_on
      new_collection.append(item)
      next
    end

    # expand variables as they are added
    variable = item.to_runner_variable
    variable[:value] = new_collection.expand_value(variable[:value], keep_undefined: keep_undefined)
    new_collection.append(variable)
  end

  new_collection
end

#to_hashObject


65
66
67
68
69
# File 'lib/gitlab/ci/variables/collection.rb', line 65

def to_hash
  self.to_runner_variables
    .to_h { |env| [env.fetch(:key), env.fetch(:value)] }
    .with_indifferent_access
end

#to_runner_variablesObject


61
62
63
# File 'lib/gitlab/ci/variables/collection.rb', line 61

def to_runner_variables
  self.map(&:to_runner_variable)
end

#to_sObject


113
114
115
# File 'lib/gitlab/ci/variables/collection.rb', line 113

def to_s
  "#{@variables_by_key.keys}, @errors='#{@errors}'"
end