class Hiera::Interpolate

Constants

INTERPOLATION
METHOD_INTERPOLATION

Public Class Methods

interpolate(data, scope, extra_data, context) click to toggle source
# File lib/hiera/interpolate.rb, line 12
def interpolate(data, scope, extra_data, context)
  if data.is_a?(String)
    # Wrapping do_interpolation in a gsub block ensures we process
    # each interpolation site in isolation using separate recursion guards.
    context ||= {}
    new_context = context.clone
    new_context[:recurse_guard] ||= Hiera::RecursiveGuard.new
    data.gsub(INTERPOLATION) do |match|
      interp_val = do_interpolation(match, scope, extra_data, new_context)

      # Get interp method in case we are aliasing
      if data.is_a?(String) && (match = data.match(INTERPOLATION))
        interpolate_method, key = get_interpolation_method_and_key(data)
      else
        interpolate_method = nil
      end

      if ( (interpolate_method == :alias_interpolate) and (!interp_val.is_a?(String)) )
        if data.match("^#{INTERPOLATION}$")
          return interp_val
        else
          raise Hiera::InterpolationInvalidValue, "Cannot call alias in the string context"
        end
      else
        interp_val
      end
    end
  else
    data
  end
end

Private Class Methods

alias_interpolate(data, key, scope, extra_data, context) click to toggle source
# File lib/hiera/interpolate.rb, line 93
def alias_interpolate(data, key, scope, extra_data, context)
  Hiera::Backend.lookup(key, nil, scope, context[:order_override], :priority, context)
end
do_interpolation(data, scope, extra_data, context) click to toggle source
# File lib/hiera/interpolate.rb, line 44
def do_interpolation(data, scope, extra_data, context)
  if data.is_a?(String) && (match = data.match(INTERPOLATION))
    interpolation_variable = match[1]
    context[:recurse_guard].check(interpolation_variable) do
      interpolate_method, key = get_interpolation_method_and_key(data)
      interpolated_data = send(interpolate_method, data, key, scope, extra_data, context)

      # Halt recursion if we encounter a literal.
      return interpolated_data if interpolate_method == :literal_interpolate

      do_interpolation(interpolated_data, scope, extra_data, context)
    end
  else
    data
  end
end
get_interpolation_method_and_key(data) click to toggle source
# File lib/hiera/interpolate.rb, line 62
def get_interpolation_method_and_key(data)
  if (match = data.match(METHOD_INTERPOLATION))
    case match[1]
    when 'hiera' then [:hiera_interpolate, match[2]]
    when 'scope' then [:scope_interpolate, match[2]]
    when 'literal' then [:literal_interpolate, match[2]]
    when 'alias' then [:alias_interpolate, match[2]]
    end
  elsif (match = data.match(INTERPOLATION))
    [:scope_interpolate, match[1]]
  end
end
hiera_interpolate(data, key, scope, extra_data, context) click to toggle source
# File lib/hiera/interpolate.rb, line 83
def hiera_interpolate(data, key, scope, extra_data, context)
  Hiera::Backend.lookup(key, nil, scope, context[:order_override], :priority, context)
end
literal_interpolate(data, key, scope, extra_data, context) click to toggle source
# File lib/hiera/interpolate.rb, line 88
def literal_interpolate(data, key, scope, extra_data, context)
  key
end
scope_interpolate(data, key, scope, extra_data, context) click to toggle source
# File lib/hiera/interpolate.rb, line 76
def scope_interpolate(data, key, scope, extra_data, context)
  segments = key.split('.')
  catch(:no_such_key) { return Hiera::Backend.qualified_lookup(segments, scope) }
  catch(:no_such_key) { Hiera::Backend.qualified_lookup(segments, extra_data) }
end