summaryrefslogtreecommitdiff
path: root/lib/expand_variables.rb
blob: 7b1533d0d321e49c710dcdb0289552d2cd2b4516 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
module ExpandVariables
  class << self
    def expand(value, variables)
      # Convert hash array to variables
      if variables.is_a?(Array)
        variables = variables.reduce({}) do |hash, variable|
          hash[variable[:key]] = variable[:value]
          hash
        end
      end

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