diff options
author | Kamil Trzciński <ayufan@ayufan.eu> | 2019-08-13 18:03:52 +0300 |
---|---|---|
committer | Kamil Trzciński <ayufan@ayufan.eu> | 2019-08-13 22:51:29 +0300 |
commit | 6150c3ff0d445c8aea1334b2547a4419be130ff5 (patch) | |
tree | faf97195c2efdde152f2b5172729f02996ce92fd /lib | |
parent | a55869483d023978655658d389aad36d63c9d2b2 (diff) |
Expand variables only when needed
This makes us to expand variables only when needed,
instead of requesting all variables each time.
This specifically helps in situation when explicit name
of `environment: production` is used.
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 |