Class: MiniHiera

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

Defined Under Namespace

Classes: Context

Constant Summary collapse

CircularReferenceError =
Class.new(RuntimeError)
KeyNotFoundError =
Class.new(RuntimeError)
CACHE =
{}
TYPES =
{
  "yaml" => Proc.new { |filename| cache(filename) { YAML.load(File.read(filename), filename) } },
  "json" => Proc.new { |filename| cache(filename) { JSON.load(File.read(filename), filename) } }
}
VERSION =
"0.1.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config, options = {}) ⇒ MiniHiera

Returns a new instance of MiniHiera.



16
17
18
19
# File 'lib/mini_hiera.rb', line 16

def initialize(config, options={})
  @config = config
  @options = options
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



15
16
17
# File 'lib/mini_hiera.rb', line 15

def config
  @config
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/mini_hiera.rb', line 15

def options
  @options
end

Class Method Details

.cache(filename) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/mini_hiera.rb', line 108

def self.cache(filename)
  if CACHE.keys.include?(filename)
    CACHE[filename]
  else
    CACHE[filename] = yield
  end
end

Instance Method Details

#context(data = {}, options = {}) ⇒ Object



129
130
131
# File 'lib/mini_hiera.rb', line 129

def context(data={}, options={})
  Context.new(self, data, @options.deep_merge(options))
end

#load_path(path) ⇒ Object



121
122
123
# File 'lib/mini_hiera.rb', line 121

def load_path(path)
  TYPES.map { |s,proc| ["#{path}.#{s}", proc] }.find_value { |filename,proc| proc.call(filename, binding) if File.exist?(filename) }
end

#replace(string, dictionary) ⇒ Object



100
101
102
103
104
# File 'lib/mini_hiera.rb', line 100

def replace(string, dictionary)
  string.gsub(/\%\{([^}]+)\}/) do |match|
    dictionary.fetch($1, match)
  end
end

#resolve(data) ⇒ Object



125
126
127
# File 'lib/mini_hiera.rb', line 125

def resolve(data)
  [data] + @options[:hierarchy].map { |path| load_path(File.join(@config, replace(path, data))) }.compact
end