2016-09-14 20:32:11 +00:00
|
|
|
module ExpandVariables
|
|
|
|
class << self
|
2016-09-14 21:00:15 +00:00
|
|
|
def expand(value, variables)
|
2016-09-14 20:32:11 +00:00
|
|
|
# Convert hash array to variables
|
|
|
|
if variables.is_a?(Array)
|
|
|
|
variables = variables.reduce({}) do |hash, variable|
|
|
|
|
hash[variable[:key]] = variable[:value]
|
|
|
|
hash
|
|
|
|
end
|
2016-09-13 12:14:55 +00:00
|
|
|
end
|
|
|
|
|
2016-09-14 20:32:11 +00:00
|
|
|
value.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/) do
|
|
|
|
variables[$1 || $2]
|
|
|
|
end
|
2016-09-13 12:14:55 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|