/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
- 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