Class: Bozo::Configuration

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

Overview

Class for generating configuration objects.

Defined Under Namespace

Classes: ConfigurationGroup

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Create a new instance.



7
8
9
10
# File 'lib/bozo/configuration.rb', line 7

def initialize
  @root = ConfigurationGroup.new
  @group_stack = [@root]
end

Instance Method Details

#[](key) ⇒ Object

Returns the configuration value for the key

Parameters:

  • key (Symbol)


36
37
38
# File 'lib/bozo/configuration.rb', line 36

def [](key)
  @root[key]
end

#apply(&block) ⇒ Object

Yields the internal binding of the configuration to the given block.

Parameters:

  • block (Proc)

    The block to yield the configuration’s internal binding to.



70
71
72
# File 'lib/bozo/configuration.rb', line 70

def apply(&block)
  @root.apply(block)
end

#group(name) ⇒ Object

Begin the definition of a group with the given name.

Parameters:

  • name (Symbol)

    The name of the group.



16
17
18
19
20
21
# File 'lib/bozo/configuration.rb', line 16

def group(name)
  new_group = @group_stack.last.ensure_child name
  @group_stack.push new_group
  yield
  @group_stack.pop
end

#inspectObject

Return the current state of the configuration.



75
76
77
# File 'lib/bozo/configuration.rb', line 75

def inspect
  @root.inspect
end

#load(path) ⇒ Object

Load the specified file as an additional configuration file.

Usage

Configuration files specify a hash of hashes in a more readable format. For example:

group :example do
  set :one, 'foo'
  set :two, 'bar'
end

Internally creates a hash like:

{:example => {:one => 'foo', :two => 'bar'}}

A configuration file can overwrite the values specified by a preceding one without error. Groups can be opened and closed as desired and nesting groups is possible.

Parameters:

  • path (String)

    The path of the configuration file.



62
63
64
# File 'lib/bozo/configuration.rb', line 62

def load(path)
  instance_eval IO.read(path), path
end

#set(key, value) ⇒ Object

Set the value of the given key within the active group.

Parameters:

  • key (Symbol)

    The key to set the value of within the active group.

  • value (Object)

    The value to set the key to within the active group.



29
30
31
# File 'lib/bozo/configuration.rb', line 29

def set(key, value)
  @group_stack.last.set_value(key, value)
end