Class: Levels::Configuration
- Inherits:
-
Object
- Object
- Levels::Configuration
- Includes:
- MethodMissing
- Defined in:
- lib/levels/configuration.rb
Overview
A Configuration is the merging of one or more levels. This is the top level object that you will interact with most.
Examples
# In this example we'll represent a Level as a Hash.
level1 = { group1: { key1: 1 } }
level2 = { group1: { key1: 2, key2: 2 }, group2: { x: 9 } }
# The configuration exposes each group that exists in a Level.
config = Levels::Configuration.new([level1, level2])
# A Group may be accessed via hash syntax.
config[:group1] # => { key1: 2, key2: 2 }
# Or method syntax.
config.group1 # => { key1: 2, key2: 2 }
# You can check if a Group exists.
config.defined?(:group1) # => true
config.group1? # => true
Instance Method Summary collapse
-
#[](group_key) ⇒ Object
Public: Retrieve a group.
-
#defined?(group_key) ⇒ Boolean
Public: Determine if a group is defined.
-
#event_handler=(event_handler) ⇒ Object
Public: Set the event handler.
-
#initialize(levels, event_handler = nil) ⇒ Configuration
constructor
Internal: Initialze a new configuration.
-
#to_enum ⇒ Object
Returns an Enumerator which yields [gruop_name, Group#to_enum].
- #to_s ⇒ Object
Methods included from MethodMissing
Constructor Details
#initialize(levels, event_handler = nil) ⇒ Configuration
Internal: Initialze a new configuration.
levels - Array of Levels::Level.
30 31 32 33 34 |
# File 'lib/levels/configuration.rb', line 30 def initialize(levels, event_handler = nil) @levels = levels @event_handler = event_handler || NullEventHandler.new @root_observer = Levels::Audit.start(LazyEvaluator.new(self)) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Levels::MethodMissing
Instance Method Details
#[](group_key) ⇒ Object
Public: Retrieve a group. The resulting group is the union of the named group from each Level that defines that group.
group_key - Symbol name of the group.
Examples
# In this example we'll represent a Level as a Hash.
level1 = { group: { key1: 1 } }
level2 = { group: { key1: 2, key2: 2 } }
config = Levels::Configuration.new([level1, level2])
config[:group] # => { key1: 2, key2: 2 }
Returns a Levels::ConfiguredGroup. Raises Levels::UnknownGroup if the group is not defined.
63 64 65 66 67 |
# File 'lib/levels/configuration.rb', line 63 def [](group_key) raise UnknownGroup unless self.defined?(group_key) group_observer = @root_observer.observe_group(@event_handler) Levels::ConfiguredGroup.new(@levels, group_key, group_observer) end |
#defined?(group_key) ⇒ Boolean
Public: Determine if a group is defined. A group is defined if it exists in any Level.
group_key - Symbol name of the group.
Returns a Boolean.
75 76 77 |
# File 'lib/levels/configuration.rb', line 75 def defined?(group_key) @levels.any? { |level| level.defined?(group_key) } end |
#event_handler=(event_handler) ⇒ Object
Public: Set the event handler. The event handler is notified whenever a key is read; allowing you to track exactly what is and isn’t used at runtime.
event_handler - Levels::EventHandler.
Returns nothing.
43 44 45 |
# File 'lib/levels/configuration.rb', line 43 def event_handler=(event_handler) @event_handler = event_handler end |
#to_enum ⇒ Object
Returns an Enumerator which yields [gruop_name, Group#to_enum].
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/levels/configuration.rb', line 84 def to_enum Enumerator.new do |y| group_keys = Set.new @levels.each do |level| level.to_enum.each do |name, group| group_keys << name end end group_keys.each do |name| y << [name, self[name].to_enum] end end end |
#to_s ⇒ Object
79 80 81 |
# File 'lib/levels/configuration.rb', line 79 def to_s "<Levels::Configuration #{@levels.map { |l| l._level_name }.join(', ')}>" end |