module Builderator::Config

Global Configuration

:nodoc

Constants

GLOBAL_DEFAULTS

Global predefined defaults

Public Class Methods

[](key, *args)
Alias for: fetch
all_layers() click to toggle source
# File lib/builderator/config.rb, line 16
def all_layers
  ([GLOBAL_DEFAULTS, defaults] + layers + [overrides, argv])
end
append(path) click to toggle source
# File lib/builderator/config.rb, line 32
def append(path)
  layers << File.from_file(path) if ::File.exist?(path)
end
Also aliased as: load
append_json(path) click to toggle source
# File lib/builderator/config.rb, line 37
def append_json(path)
  layers << File.from_json(path) if ::File.exist?(path)
end
Also aliased as: load_json
argv(options = {}) click to toggle source
# File lib/builderator/config.rb, line 28
def argv(options = {})
  @argv ||= File.new(options, :source => 'argv')
end
compile(max_iterations = 6) click to toggle source

The compile method renders a single File instance from all of the configured input layers. It follows the following algorithm:

> `DIRTY` is defined as the logical OR of the dirty state of each layer.

Layers are responsible for detecting changes to their own properties
while being compiled.

> LOOP unitl not DIRTY plus 1 iteration

1. Call each layer's own compile method.
2. For each layer, merge it into the COMPILED output.
FAIL if ITERATIONS > LIMIT

> The additional iteration after DIRTY becomes false is to ensure that

any changes to the compiled output during the final merge are passed
back through each layer's compile.
# File lib/builderator/config.rb, line 67
def compile(max_iterations = 6)
  compiled.unseal
  compile_iterations = 0
  break_break = false

  ## Inject GLOBAL_DEFAULTS before starting compile
  compiled.merge(GLOBAL_DEFAULTS.compile)

  ## Automatically recompile while layers are dirty
  loop do
    fail "Re-compile iteration limit of #{max_iterations} has been exceeded. "\
         "#{all_layers.select(&:dirty).map(&:source).join(', ')} are dirty." if compile_iterations >= max_iterations

    ## Merge layers from lowest to highest. Compile, then merge.
    all_layers.each do |layer|
      layer.compile
    end

    all_layers.each do |layer|
      layer.policies.each { |_, policy| compiled.merge(policy) }

      ## Merge layer after its policy documents to allow overides
      compiled.merge(layer)
    end

    break if break_break && !dirty?

    break_break = !dirty?
    compile_iterations += 1
  end

  ## Don't auto-populate keys anymore
  compiled.seal
end
Also aliased as: recompile
compiled() click to toggle source
# File lib/builderator/config.rb, line 107
def compiled
  @compiled ||= File.new({}, :source => 'compiled')
end
defaults() click to toggle source
# File lib/builderator/config.rb, line 20
def defaults
  @defaults ||= File.new({}, :source => 'defaults')
end
dirty?() click to toggle source
# File lib/builderator/config.rb, line 103
def dirty?
  all_layers.any?(&:dirty)
end
fetch(key, *args) click to toggle source
# File lib/builderator/config.rb, line 121
def fetch(key, *args)
  compiled.send(key, *args)
end
Also aliased as: []
layers() click to toggle source

GLOBAL_DEFAULTS is the lowest-precedence layer, followed by dynamically defined instance-defaults.

# File lib/builderator/config.rb, line 12
def layers
  @layers ||= []
end
load(path)
Alias for: append
load_json(path)
Alias for: append_json
method_missing(method_name, *args) click to toggle source
Calls superclass method
# File lib/builderator/config.rb, line 126
def method_missing(method_name, *args)
  return super unless compiled.respond_to?(method_name)

  compiled.send(method_name, *args)
end
overrides() click to toggle source
# File lib/builderator/config.rb, line 24
def overrides
  @overrides ||= File.new({}, :source => 'overrides')
end
prepend(path) click to toggle source
# File lib/builderator/config.rb, line 42
def prepend(path)
  layers.unshift(File.from_file(path)) if ::File.exist?(path)
end
prepend_json(path) click to toggle source
# File lib/builderator/config.rb, line 46
def prepend_json(path)
  layers.unshift(File.from_json(path)) if ::File.exist?(path)
end
recompile(max_iterations = 6)
Alias for: compile
reset!() click to toggle source
# File lib/builderator/config.rb, line 111
def reset!
  @layers = []

  @defaults = File.new({}, :source => 'defaults')
  @overrides = File.new({}, :source => 'overrides')
  @argv = File.new({}, :source => 'argv')

  @compiled = File.new({}, :source => 'compiled')
end