Class: Levels::Setup

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

Defined Under Namespace

Classes: Input, NullInput

Instance Method Summary collapse

Constructor Details

#initializeSetup

Internal: Initialize a new Levels::Setup.



5
6
7
# File 'lib/levels/setup.rb', line 5

def initialize
  @inputs = []
end

Instance Method Details

#add(name, source) ⇒ Object

Public: Add a level of configuration.

name - String name of the level. source - Anything that can be identified as an input source. File

path, code or any object that responds to #read is a valid
source.

Returns nothing.



17
18
19
20
21
# File 'lib/levels/setup.rb', line 17

def add(name, source)
  level = -> { Levels::Level.new(name) }
  input = Input.new(source)
  @inputs << [level, input]
end

#add_level(level) ⇒ Object

Public: Add an already initialized Level.

level - Levels::Level.

Returns nothing.



44
45
46
# File 'lib/levels/setup.rb', line 44

def add_level(level)
  @inputs << [-> { level }, NullInput.new]
end

#add_system(prefix = nil, name = nil, env_hash = ENV) ⇒ Object

Public: Add the system environment as a level of configuration.

prefix - String the prefix for environment variables (default

none).

name - String the name of the level (default: “System

Environment").

env_hash - Hash of environment variables (default: ENV).

Returns nothing.



32
33
34
35
36
37
# File 'lib/levels/setup.rb', line 32

def add_system(prefix = nil, name = nil, env_hash = ENV)
  key_formatter = Levels::System::KeyFormatter.new(prefix)
  level = -> { Levels::Level.new(name || "System Environment") }
  input = -> template { Levels::Input::System.new(template, key_formatter, env_hash) }
  @inputs << [level, input]
end

#mergeObject

Public: Parse all inputs sources and get a Configuration.

Returns a Levels::Configuration.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/levels/setup.rb', line 51

def merge
  levels = []
  @inputs.each do |level_proc, input|
    level = level_proc.call
    case input
    when Proc
      template = Levels::Configuration.new(levels)
      input = input.call(template.to_enum)
      input.read(level)
    else
      input.read(level)
    end
    levels << level
  end
  Levels::Configuration.new(levels)
end