Class: Gitlab::Ci::Variables::Collection::Item
- Inherits:
-
Object
- Object
- Gitlab::Ci::Variables::Collection::Item
- Includes:
- Utils::StrongMemoize
- Defined in:
- lib/gitlab/ci/variables/collection/item.rb
Constant Summary collapse
- VARIABLES_REGEXP =
/\$\$|%%|\$(?<key>[a-zA-Z_][a-zA-Z0-9_]*)|\${\g<key>?}|%\g<key>%/- VARIABLE_REF_CHARS =
%w[$ %].freeze
Class Method Summary collapse
Instance Method Summary collapse
- #==(other) ⇒ Object
- #[](key) ⇒ Object
- #depends_on ⇒ Object
- #file? ⇒ Boolean
-
#initialize(key:, value:, public: true, file: false, masked: false, raw: false) ⇒ Item
constructor
A new instance of Item.
- #key ⇒ Object
- #masked? ⇒ Boolean
- #raw? ⇒ Boolean (also: #raw)
- #to_hash_variable ⇒ Object
-
#to_runner_variable ⇒ Object
If ‘file: true` or `raw: true` has been provided we expose it, otherwise we don’t expose
fileandrawattributes at all (stems from what the runner expects). -
#to_s ⇒ Object
This is for debugging purposes only.
- #value ⇒ Object
Constructor Details
#initialize(key:, value:, public: true, file: false, masked: false, raw: false) ⇒ Item
Returns a new instance of Item.
13 14 15 16 17 18 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 13 def initialize(key:, value:, public: true, file: false, masked: false, raw: false) raise ArgumentError, "`#{key}` must be of type String or nil value, while it was: #{value.class}" unless value.is_a?(String) || value.nil? @variable = { key: key, value: value, public: public, file: file, masked: masked, raw: raw } end |
Class Method Details
.fabricate(resource) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 76 def self.fabricate(resource) case resource when Hash self.new(**resource.symbolize_keys) when ::Ci::HasVariable self.new(**resource.to_hash_variable) when self resource.dup else raise ArgumentError, "Unknown `#{resource.class}` variable resource!" end end |
.possible_var_reference?(value) ⇒ Boolean
89 90 91 92 93 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 89 def self.possible_var_reference?(value) return unless value VARIABLE_REF_CHARS.any? { |symbol| value.include?(symbol) } end |
Instance Method Details
#==(other) ⇒ Object
45 46 47 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 45 def ==(other) to_runner_variable == self.class.fabricate(other).to_runner_variable end |
#[](key) ⇒ Object
41 42 43 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 41 def [](key) @variable.fetch(key) end |
#depends_on ⇒ Object
49 50 51 52 53 54 55 56 57 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 49 def depends_on strong_memoize(:depends_on) do next if raw? next unless self.class.possible_var_reference?(value) value.scan(VARIABLES_REGEXP).filter_map(&:last) end end |
#file? ⇒ Boolean
33 34 35 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 33 def file? @variable.fetch(:file) end |
#key ⇒ Object
20 21 22 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 20 def key @variable.fetch(:key) end |
#masked? ⇒ Boolean
37 38 39 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 37 def masked? @variable.fetch(:masked) end |
#raw? ⇒ Boolean Also known as: raw
28 29 30 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 28 def raw? @variable.fetch(:raw) end |
#to_hash_variable ⇒ Object
72 73 74 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 72 def to_hash_variable @variable end |
#to_runner_variable ⇒ Object
If ‘file: true` or `raw: true` has been provided we expose it, otherwise we don’t expose file and raw attributes at all (stems from what the runner expects).
This method should only be called via runner_variables->to_runner_variables->to_runner_variable because this is an expensive operation by initializing a new object.
66 67 68 69 70 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 66 def to_runner_variable @variable.reject do |hash_key, hash_value| (hash_key == :file || hash_key == :raw) && hash_value == false end end |
#to_s ⇒ Object
This is for debugging purposes only.
96 97 98 99 100 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 96 def to_s return to_hash_variable.to_s unless depends_on "#{to_hash_variable}, depends_on=#{depends_on}" end |
#value ⇒ Object
24 25 26 |
# File 'lib/gitlab/ci/variables/collection/item.rb', line 24 def value @variable.fetch(:value) end |