Class: Hiera::Interpolate

Inherits:
Object
  • Object
show all
Defined in:
lib/hiera/interpolate.rb

Constant Summary collapse

INTERPOLATION =
/%\{([^\}]*)\}/
METHOD_INTERPOLATION =
/%\{(scope|hiera)\(['"]([^"']*)["']\)\}/

Class Method Summary collapse

Class Method Details

.get_interpolation_method_and_key(data) ⇒ Object



21
22
23
24
25
26
27
28
29
30
# File 'lib/hiera/interpolate.rb', line 21

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]]
    end
  elsif (match = data.match(INTERPOLATION))
    [:scope_interpolate, match[1]]
  end
end

.hiera_interpolate(data, key, scope, extra_data) ⇒ Object



42
43
44
45
# File 'lib/hiera/interpolate.rb', line 42

def hiera_interpolate(data, key, scope, extra_data)
  value = Hiera::Backend.lookup(key, nil, scope, nil, :priority)
  data.sub(METHOD_INTERPOLATION, value)
end

.interpolate(data, recurse_guard, scope, extra_data) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/hiera/interpolate.rb', line 8

def interpolate(data, recurse_guard, scope, extra_data)
  if data.is_a?(String) && (match = data.match(INTERPOLATION))
    interpolation_variable = match[1]
    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)
      interpolate(interpolated_data, recurse_guard, scope, extra_data)
    end
  else
    data
  end
end

.scope_interpolate(data, key, scope, extra_data) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/hiera/interpolate.rb', line 33

def scope_interpolate(data, key, scope, extra_data)
  value = scope[key]
  if value.nil? || value == :undefined
    value = extra_data[key]
  end
  data.sub(INTERPOLATION, value.to_s)
end