Class: Gitlab::Ci::Config::Normalizer::MatrixStrategy

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/ci/config/normalizer/matrix_strategy.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_name, instance, variables, total) ⇒ MatrixStrategy

Returns a new instance of MatrixStrategy.


35
36
37
38
39
40
# File 'lib/gitlab/ci/config/normalizer/matrix_strategy.rb', line 35

def initialize(job_name, instance, variables, total)
  @job_name = job_name
  @instance = instance
  @variables = variables.to_h
  @total = total
end

Class Method Details

.applies_to?(config) ⇒ Boolean

Returns:

  • (Boolean)

9
10
11
# File 'lib/gitlab/ci/config/normalizer/matrix_strategy.rb', line 9

def applies_to?(config)
  config.is_a?(Hash) && config.key?(:matrix)
end

.build_from(job_name, initial_config) ⇒ Object


13
14
15
16
17
18
19
20
# File 'lib/gitlab/ci/config/normalizer/matrix_strategy.rb', line 13

def build_from(job_name, initial_config)
  config = expand(initial_config[:matrix])
  total = config.size

  config.map.with_index do |vars, index|
    new(job_name, index.next, vars, total)
  end
end

.expand(config) ⇒ Object

rubocop: disable CodeReuse/ActiveRecord


23
24
25
26
27
28
29
30
31
# File 'lib/gitlab/ci/config/normalizer/matrix_strategy.rb', line 23

def expand(config)
  config.flat_map do |config|
    values = config.values

    values[0]
      .product(*values.from(1))
      .map { |vals| config.keys.zip(vals).to_h }
  end
end

Instance Method Details

#attributesObject


42
43
44
45
46
47
48
49
# File 'lib/gitlab/ci/config/normalizer/matrix_strategy.rb', line 42

def attributes
  {
    name: name,
    instance: instance,
    variables: variables,
    parallel: { total: total }
  }
end

#nameObject


51
52
53
54
55
56
57
58
# File 'lib/gitlab/ci/config/normalizer/matrix_strategy.rb', line 51

def name
  vars = variables
    .values
    .compact
    .join(', ')

  "#{job_name}: [#{vars}]"
end