summaryrefslogtreecommitdiff
path: root/lib/expand_variables.rb
blob: c83cec9dc4a4e30fa65c23b8a8f52e6d5139d36d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# frozen_string_literal: true

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