debian-mirror-gitlab/lib/expand_variables.rb

32 lines
747 B
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2016-09-29 09:46:39 +05:30
module ExpandVariables
class << self
def expand(value, variables)
2019-10-12 21:52:04 +05:30
variables_hash = nil
value.gsub(/\$([a-zA-Z_][a-zA-Z0-9_]*)|\${\g<1>}|%\g<1>%/) do
variables_hash ||= transform_variables(variables)
2020-11-24 15:15:51 +05:30
variables_hash[Regexp.last_match(1) || Regexp.last_match(2)]
2019-10-12 21:52:04 +05:30
end
end
private
def transform_variables(variables)
# Lazily initialise variables
variables = variables.call if variables.is_a?(Proc)
2016-09-29 09:46:39 +05:30
# Convert hash array to variables
if variables.is_a?(Array)
variables = variables.reduce({}) do |hash, variable|
hash[variable[:key]] = variable[:value]
hash
end
end
2019-10-12 21:52:04 +05:30
variables
2016-09-29 09:46:39 +05:30
end
end
end