Class: MiniHiera::Context

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

Constant Summary collapse

DefaultObject =
Object.new

Instance Method Summary collapse

Constructor Details

#initialize(hiera, data, options) ⇒ Context

Returns a new instance of Context.



22
23
24
# File 'lib/mini_hiera.rb', line 22

def initialize(hiera, data, options)
  @hiera, @data, @options = hiera, data, options
end

Instance Method Details

#[](key) ⇒ Object



88
89
90
# File 'lib/mini_hiera.rb', line 88

def [](key)
  fetch(key)
end

#detect_circular_interpolation(key) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/mini_hiera.rb', line 46

def detect_circular_interpolation(key)
  Thread.current[:interpolation_stack] ||= []
  raise CircularReferenceError, "Circular reference when resolving key '#{key}'" if Thread.current[:interpolation_stack].include?(key)
  Thread.current[:interpolation_stack] << key
  yield
ensure
  Thread.current[:interpolation_stack].pop
end

#error_message_suffixObject



40
41
42
43
44
# File 'lib/mini_hiera.rb', line 40

def error_message_suffix
  if !!(v = Thread.current[:interpolation_stack]) && !v.empty?
    "whilst interpolating keys #{v.reverse.join(", ")}"
  end
end

#fetch(key, default = DefaultObject, &default_block) ⇒ Object

Raises:

  • (ArgumentError)


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mini_hiera.rb', line 60

def fetch(key, default=DefaultObject, &default_block)
  key = key.to_s

  raise ArgumentError, "default value and block specified" if default != DefaultObject && default_block

  default_block ||= lambda { default == DefaultObject ? raise(KeyNotFoundError, [" ** Unknown key '#{key}'", error_message_suffix].join(" ")) : default }

  default_block_wrapper = lambda { |*_| default_block.arity == 0 ? default_block.call : default_block.call(key) }

  data = hiera_data

  key.split(".").each do |k|
    if data.is_a?(Array)
      raise TypeError, "no implicit conversion of String into Integer" unless k.to_i.to_s == k
      data = data[k.to_i]
    elsif data.is_a?(Hash)
      data = data.fetch(k, &default_block_wrapper)
    else
      data = default_block_wrapper.call
      break
    end
  end

  detect_circular_interpolation(key) do
    postprocess(data)
  end
end

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


92
93
94
95
96
97
# File 'lib/mini_hiera.rb', line 92

def has_key?(key)
  fetch(key)
  true
rescue MiniHiera::KeyNotFoundError
  false
end

#hiera_dataObject



55
56
57
# File 'lib/mini_hiera.rb', line 55

def hiera_data
  @hiera_data ||= @hiera.resolve(@data).inject({}) { |a,v| a.deep_merge(v, @options.fetch(:deep_merge, {})) }
end

#postprocess(value) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/mini_hiera.rb', line 26

def postprocess(value)
  if value.is_a?(String)
    value.gsub(/\%\{([^}]+)\}/) do |match|
      fetch($1)
    end
  elsif value.is_a?(Hash)
    Hash[value.map { |k,v| [k, postprocess(v)] }]
  elsif value.is_a?(Array)
    value.map { |v| postprocess(v) }
  else
    value
  end
end