Class: Bozo::Configuration
- Inherits:
-
Object
- Object
- Bozo::Configuration
- Defined in:
- lib/bozo/configuration.rb
Overview
Class for generating configuration objects.
Defined Under Namespace
Classes: ConfigurationGroup
Instance Method Summary collapse
-
#[](key) ⇒ Object
Returns the configuration value for the key.
-
#apply(&block) ⇒ Object
Yields the internal binding of the configuration to the given block.
-
#group(name) ⇒ Object
Begin the definition of a group with the given name.
-
#initialize ⇒ Configuration
constructor
Create a new instance.
-
#inspect ⇒ Object
Return the current state of the configuration.
-
#load(path) ⇒ Object
Load the specified file as an additional configuration file.
-
#set(key, value) ⇒ Object
Set the value of the given key within the active group.
Constructor Details
#initialize ⇒ Configuration
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
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.
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.
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 |
#inspect ⇒ Object
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.
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.
29 30 31 |
# File 'lib/bozo/configuration.rb', line 29 def set(key, value) @group_stack.last.set_value(key, value) end |