Class: Levels::Level
- Inherits:
-
Object
- Object
- Levels::Level
- Includes:
- MethodMissing
- Defined in:
- lib/levels/level.rb
Overview
A Level is a named set of groups. A Configuration is made up of multiple levels with clear semantics on how those levels are merged. You generally won’t instantiate a Level directly, but instead load one from an external source.
Examples
level = Levels::Level.new("My Level")
level.set_group(:group1, a: 1, b: 2)
level.set_group(:group2, c: 3, d: 4)
level.group1 # => { a: 1, b: 2 }
level.group2 # => { c: 3, d: 4 }
Instance Method Summary collapse
-
#[](group_name) ⇒ Object
Public: Get a group by name.
- #_level_name ⇒ Object
-
#defined?(group_name) ⇒ Boolean
Public: Determine if a group has been defined.
- #eql_hash?(hash) ⇒ Boolean
-
#initialize(name) ⇒ Level
constructor
Internal: Initialize a new level.
-
#set_group(group_name, hash) ⇒ Object
Internal: Define a group.
-
#to_enum ⇒ Object
Returns an Enumerator which yields [group_name, Group#to_enum].
- #to_s ⇒ Object
Methods included from MethodMissing
Constructor Details
#initialize(name) ⇒ Level
Internal: Initialize a new level.
name - String name of the level.
23 24 25 26 |
# File 'lib/levels/level.rb', line 23 def initialize(name) @name = name @groups = Levels::KeyValues.new end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method in the class Levels::MethodMissing
Instance Method Details
#[](group_name) ⇒ Object
Public: Get a group by name.
group_name - Symbol name of the group.
Returns a Levels::Group. Raises Levels::UnknownGroup if the group is not defined.
34 35 36 |
# File 'lib/levels/level.rb', line 34 def [](group_name) @groups[group_name] or raise UnknownGroup, "#{group_name.inspect} group is not defined" end |
#_level_name ⇒ Object
71 72 73 |
# File 'lib/levels/level.rb', line 71 def _level_name @name end |
#defined?(group_name) ⇒ Boolean
Public: Determine if a group has been defined.
Returns a Boolean.
41 42 43 |
# File 'lib/levels/level.rb', line 41 def defined?(group_name) @groups.key?(group_name) end |
#eql_hash?(hash) ⇒ Boolean
75 76 77 78 |
# File 'lib/levels/level.rb', line 75 def eql_hash?(hash) key_values = Levels::KeyValues.new(hash) @groups.all? { |name, group| group.eql_hash?(key_values[name]) } end |
#set_group(group_name, hash) ⇒ Object
Internal: Define a group.
group_name - Symbol name of the group. hash - Hash of values.
Returns nothing.
51 52 53 54 55 56 |
# File 'lib/levels/level.rb', line 51 def set_group(group_name, hash) if @groups.key?(group_name) raise DuplicateGroup, "#{group_name} has already been defined" end @groups[group_name] = Group.new(hash) end |
#to_enum ⇒ Object
Returns an Enumerator which yields [group_name, Group#to_enum].
63 64 65 66 67 68 69 |
# File 'lib/levels/level.rb', line 63 def to_enum Enumerator.new do |y| @groups.each do |name, group| y << [name.to_sym, group.to_enum] end end end |
#to_s ⇒ Object
58 59 60 |
# File 'lib/levels/level.rb', line 58 def to_s "<Levels::Level #{@name.inspect}>" end |