Class: MiniHiera::Context
- Inherits:
-
Object
- Object
- MiniHiera::Context
- Defined in:
- lib/mini_hiera.rb
Constant Summary collapse
- DefaultObject =
Object.new
Instance Method Summary collapse
- #[](key) ⇒ Object
- #detect_circular_interpolation(key) ⇒ Object
- #error_message_suffix ⇒ Object
- #fetch(key, default = DefaultObject, &default_block) ⇒ Object
- #has_key?(key) ⇒ Boolean
- #hiera_data ⇒ Object
-
#initialize(hiera, data, options) ⇒ Context
constructor
A new instance of Context.
- #postprocess(value) ⇒ Object
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, ) @hiera, @data, @options = hiera, data, 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_suffix ⇒ Object
40 41 42 43 44 |
# File 'lib/mini_hiera.rb', line 40 def if !!(v = Thread.current[:interpolation_stack]) && !v.empty? "whilst interpolating keys #{v.reverse.join(", ")}" end end |
#fetch(key, default = DefaultObject, &default_block) ⇒ Object
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}'", ].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
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_data ⇒ Object
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 |