PageRenderTime 20ms CodeModel.GetById 17ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 1ms

/lib/expand_variables.rb

https://gitlab.com/vicvega/gitlab-ce
Ruby | 17 lines | 15 code | 1 blank | 1 comment | 2 complexity | d8790079406855c403d119a05a2693e8 MD5 | raw file
 1module ExpandVariables
 2  class << self
 3    def expand(value, variables)
 4      # Convert hash array to variables
 5      if variables.is_a?(Array)
 6        variables = variables.reduce({}) do |hash, variable|
 7          hash[variable[:key]] = variable[:value]
 8          hash
 9        end
10      end
11
12      value.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/) do
13        variables[$1 || $2]
14      end
15    end
16  end
17end