summaryrefslogtreecommitdiff
path: root/lib/expand_variables.rb
diff options
context:
space:
mode:
authorJeremy Watson <jwatson@gitlab.com>2019-08-21 10:43:30 -0400
committerJeremy Watson <jwatson@gitlab.com>2019-08-21 10:43:30 -0400
commitaec4ce4ac538992ae09c8a9c77be62a22ea239f1 (patch)
tree0ec040a1d020a0096f60f1363db7fd1751967e9c /lib/expand_variables.rb
parentad799726ae697d12664b8c3903e8297e7bfb4088 (diff)
parentef0f1509dd2a2a3ba5798362e2be21108b705a85 (diff)
downloadgitlab-ce-docs-group-managed-accounts.tar.gz
Merge branch 'master' of gitlab.com:gitlab-org/gitlab-ce into docs-group-managed-accountsdocs-group-managed-accounts
Diffstat (limited to 'lib/expand_variables.rb')
-rw-r--r--lib/expand_variables.rb18
1 files changed, 15 insertions, 3 deletions
diff --git a/lib/expand_variables.rb b/lib/expand_variables.rb
index c83cec9dc4a..45af30f46dc 100644
--- a/lib/expand_variables.rb
+++ b/lib/expand_variables.rb
@@ -3,6 +3,20 @@
module ExpandVariables
class << self
def expand(value, variables)
+ variables_hash = nil
+
+ value.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/) do
+ variables_hash ||= transform_variables(variables)
+ variables_hash[$1 || $2]
+ end
+ end
+
+ private
+
+ def transform_variables(variables)
+ # Lazily initialise variables
+ variables = variables.call if variables.is_a?(Proc)
+
# Convert hash array to variables
if variables.is_a?(Array)
variables = variables.reduce({}) do |hash, variable|
@@ -11,9 +25,7 @@ module ExpandVariables
end
end
- value.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/) do
- variables[$1 || $2]
- end
+ variables
end
end
end