Class: Gitlab::Ci::Variables::Collection
- Inherits:
-
Object
- Object
- Gitlab::Ci::Variables::Collection
- 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
Instance Attribute Summary collapse
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
Class Method Summary collapse
Instance Method Summary collapse
- #+(other) ⇒ Object
- #[](key) ⇒ Object
- #all(key) ⇒ Object
- #append(resource) ⇒ Object
- #compact ⇒ Object
- #concat(resources) ⇒ Object
- #each ⇒ Object
-
#initialize(variables = [], errors = nil) ⇒ Collection
constructor
A new instance of Collection.
- #reject(&block) ⇒ Object
- #size ⇒ Object
- #sort_and_expand_all(keep_undefined: false, expand_file_refs: true, expand_raw_refs: true) ⇒ Object
- #to_hash ⇒ Object
- #to_runner_variables ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(variables = [], errors = nil) ⇒ Collection
Returns a new instance of Collection.
26 27 28 29 30 31 32 |
# File 'lib/gitlab/ci/variables/collection.rb', line 26 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
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
9 10 11 |
# File 'lib/gitlab/ci/variables/collection.rb', line 9 def errors @errors end |
Class Method Details
.fabricate(input) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
# File 'lib/gitlab/ci/variables/collection.rb', line 11 def self.fabricate(input) case input when Array new(input) when Hash new(input.map { |key, value| { key: key, value: value } }) when Proc fabricate(input.call) when self input else raise ArgumentError, "Unknown `#{input.class}` variable collection!" end end |
Instance Method Details
#+(other) ⇒ Object
56 57 58 59 60 61 |
# File 'lib/gitlab/ci/variables/collection.rb', line 56 def +(other) self.class.new.tap do |collection| self.each { |variable| collection.append(variable) } other.each { |variable| collection.append(variable) } end end |
#[](key) ⇒ Object
63 64 65 |
# File 'lib/gitlab/ci/variables/collection.rb', line 63 def [](key) all(key)&.last end |
#all(key) ⇒ Object
67 68 69 70 |
# File 'lib/gitlab/ci/variables/collection.rb', line 67 def all(key) vars = @variables_by_key[key] vars unless vars.empty? end |
#append(resource) ⇒ Object
34 35 36 37 38 39 40 |
# File 'lib/gitlab/ci/variables/collection.rb', line 34 def append(resource) item = Collection::Item.fabricate(resource) @variables.append(item) @variables_by_key[item[:key]] << item self end |
#compact ⇒ Object
42 43 44 |
# File 'lib/gitlab/ci/variables/collection.rb', line 42 def compact Collection.new(select { |variable| !variable.value.nil? }) end |
#concat(resources) ⇒ Object
46 47 48 49 50 |
# File 'lib/gitlab/ci/variables/collection.rb', line 46 def concat(resources) return self if resources.nil? tap { resources.each { |variable| self.append(variable) } } end |
#each ⇒ Object
52 53 54 |
# File 'lib/gitlab/ci/variables/collection.rb', line 52 def each @variables.each { |variable| yield variable } end |
#reject(&block) ⇒ Object
86 87 88 |
# File 'lib/gitlab/ci/variables/collection.rb', line 86 def reject(&block) Collection.new(@variables.reject(&block)) end |
#size ⇒ Object
72 73 74 |
# File 'lib/gitlab/ci/variables/collection.rb', line 72 def size @variables.size end |
#sort_and_expand_all(keep_undefined: false, expand_file_refs: true, expand_raw_refs: true) ⇒ Object
90 91 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 90 def (keep_undefined: false, expand_file_refs: true, expand_raw_refs: true) 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.(variable[:value], keep_undefined: keep_undefined, expand_file_refs: , expand_raw_refs: ) new_collection.append(variable) end new_collection end |
#to_hash ⇒ Object
80 81 82 83 84 |
# File 'lib/gitlab/ci/variables/collection.rb', line 80 def to_hash self.to_runner_variables .to_h { |env| [env.fetch(:key), env.fetch(:value)] } .with_indifferent_access end |
#to_runner_variables ⇒ Object
76 77 78 |
# File 'lib/gitlab/ci/variables/collection.rb', line 76 def to_runner_variables self.map(&:to_runner_variable) end |
#to_s ⇒ Object
113 114 115 |
# File 'lib/gitlab/ci/variables/collection.rb', line 113 def to_s "#{@variables_by_key.keys}, @errors='#{@errors}'" end |