diff options
author | Stan Hu <stanhu@gmail.com> | 2019-08-14 00:14:23 +0300 |
---|---|---|
committer | Stan Hu <stanhu@gmail.com> | 2019-08-14 00:14:23 +0300 |
commit | e2361565ceb1754bd63c2d98a5c1cfe134e93160 (patch) | |
tree | 4182258751958245b91e0e45701e374ed77aa53a /lib | |
parent | f7ac41060c4432fa56a554250c72a04a197c3e05 (diff) | |
parent | 6150c3ff0d445c8aea1334b2547a4419be130ff5 (diff) |
Merge branch 'expand-variables-only-when-needed' into 'master'
Expand variables only when needed
See merge request gitlab-org/gitlab-ce!31772
Diffstat (limited to 'lib')
-rw-r--r-- | lib/expand_variables.rb | 18 |
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 |