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.
Instance Method Summary collapse
- #+(other) ⇒ Object
- #[](key) ⇒ Object
- #all(key) ⇒ Object
- #append(resource) ⇒ Object
- #compact ⇒ Object
- #concat(resources) ⇒ Object
- #each ⇒ Object
- #expand_value(value, keep_undefined: false) ⇒ Object
-
#initialize(variables = [], errors = nil) ⇒ Collection
constructor
A new instance of Collection.
- #reject(&block) ⇒ Object
- #size ⇒ Object
- #sort_and_expand_all(keep_undefined: false) ⇒ Object
- #to_hash ⇒ Object
- #to_runner_variables ⇒ Object
- #to_s ⇒ Object
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
#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 |
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 |
#compact ⇒ Object
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 |
#each ⇒ Object
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 (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 |
#size ⇒ Object
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 (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.(variable[:value], keep_undefined: keep_undefined) new_collection.append(variable) end new_collection end |
#to_hash ⇒ Object
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_variables ⇒ Object
61 62 63 |
# File 'lib/gitlab/ci/variables/collection.rb', line 61 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 |