BigW Consortium Gitlab

expand_variables.rb 417 Bytes
Newer Older
1 2
module ExpandVariables
  class << self
3
    def expand(value, variables)
4 5 6 7 8 9
      # Convert hash array to variables
      if variables.is_a?(Array)
        variables = variables.reduce({}) do |hash, variable|
          hash[variable[:key]] = variable[:value]
          hash
        end
10 11
      end

12 13 14
      value.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/) do
        variables[$1 || $2]
      end
15 16 17
    end
  end
end