module Tins::StringNamedPlaceholders

def named_placeholders

Returns:
  • (Array) - An array of unique symbol representations of the
def named_placeholders
  scan(/%\{([^}]+)\}/).inject([], &:concat).uniq.map(&:to_sym)
end

def named_placeholders_assign(hash, default: nil)

Returns:
  • (Hash) - A new hash containing the assigned values for each named

Parameters:
  • default (Object, Proc) -- The default value to use for placeholders not present in the hash.
  • hash (Hash) -- A hash mapping placeholder names to their corresponding values.
def named_placeholders_assign(hash, default: nil)
  hash = hash.transform_keys(&:to_sym)
  named_placeholders.each_with_object({}) do |placeholder, h|
    h[placeholder] = hash[placeholder] ||
      (default.is_a?(Proc) ? default.(placeholder) : default)
  end
end

def named_placeholders_interpolate(hash, default: nil)

Returns:
  • (String) - A new string with named placeholders replaced by their values

Parameters:
  • default (Object, Proc) -- The default value to use for placeholders not present in the hash
  • hash (Hash) -- A hash mapping placeholder names to their corresponding values
def named_placeholders_interpolate(hash, default: nil)
  values = named_placeholders_assign(hash, default:)
  self % values
end