Commit 2ff8668b authored by Marius Bobin's avatar Marius Bobin

Implement maintainer feedback

Refactor the code for readability and add a missing test case
parent 78845b8f
...@@ -5,27 +5,35 @@ module ExpandVariables ...@@ -5,27 +5,35 @@ module ExpandVariables
class << self class << self
def expand(value, variables) def expand(value, variables)
variables_hash = nil replace_with(value, variables) do |vars_hash, last_match|
match_or_blank_value(vars_hash, last_match)
value.gsub(VARIABLES_REGEXP) do
variables_hash ||= transform_variables(variables)
variables_hash[Regexp.last_match(1) || Regexp.last_match(2)]
end end
end end
def expand_existing(value, variables) def expand_existing(value, variables)
replace_with(value, variables) do |vars_hash, last_match|
match_or_original_value(vars_hash, last_match)
end
end
private
def replace_with(value, variables)
variables_hash = nil variables_hash = nil
value.gsub(VARIABLES_REGEXP) do value.gsub(VARIABLES_REGEXP) do
variables_hash ||= transform_variables(variables) variables_hash ||= transform_variables(variables)
variables_hash.fetch( yield(variables_hash, Regexp.last_match)
Regexp.last_match(1) || Regexp.last_match(2),
Regexp.last_match(0)
)
end end
end end
private def match_or_blank_value(variables, last_match)
variables[last_match[1] || last_match[2]]
end
def match_or_original_value(variables, last_match)
match_or_blank_value(variables, last_match) || last_match[0]
end
def transform_variables(variables) def transform_variables(variables)
# Lazily initialise variables # Lazily initialise variables
......
...@@ -113,6 +113,13 @@ RSpec.describe ExpandVariables do ...@@ -113,6 +113,13 @@ RSpec.describe ExpandVariables do
variables: [ variables: [
{ key: 'variable', value: 'value' } { key: 'variable', value: 'value' }
] ]
},
"complex expansions with missing variable for Windows": {
value: 'key%variable%%variable2%',
result: 'keyvalue',
variables: [
{ key: 'variable', value: 'value' }
]
} }
} }
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment