class SecretConfig::StringInterpolator

Public Class Methods

new(pattern = nil) click to toggle source
# File lib/secret_config/string_interpolator.rb, line 13
def initialize(pattern = nil)
  @pattern = pattern || /\${1,2}\{([^}]+)\}/
end

Public Instance Methods

parse(string) click to toggle source
# File lib/secret_config/string_interpolator.rb, line 17
def parse(string)
  string.gsub(/\${1,2}\{([^}]+)\}/) do |match|
    if match.start_with?("$$")
      match[1..-1]
    else
      expr          = Regexp.last_match(1) || Regexp.last_match(2) || match.tr("${}", "")
      key, args_str = expr.split(":")
      key           = key.strip.to_sym
      arguments     = args_str&.split(",")&.map { |v| v.strip == "" ? nil : v.strip } || []
      raise(InvalidInterpolation, "Invalid key: #{key} in string: #{match}") unless respond_to?(key)

      public_send(key, *arguments)
    end
  end
end