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

module ExpandVariables
  class << self
    def expand(value, variables)
      # Convert hash array to variables
      if variables.is_a?(Array)
        variables = variables.each_with_object({}) { |variable, hash|
          hash[variable[:key]] = variable[:value]
        }
      end

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