Class: Logbert::LevelManager
- Inherits:
-
Module
- Object
- Module
- Logbert::LevelManager
- Defined in:
- lib/logbert/levels.rb
Overview
This class doubles as a mixin. Bazinga!
Instance Method Summary collapse
- #[](x) ⇒ Object
- #define_level(name, value) ⇒ Object
-
#initialize ⇒ LevelManager
constructor
A new instance of LevelManager.
- #levels ⇒ Object
- #names ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize ⇒ LevelManager
Returns a new instance of LevelManager.
29 30 31 32 33 34 35 36 |
# File 'lib/logbert/levels.rb', line 29 def initialize @name_to_level = {} @value_to_level = {} @quick_lookup = {} Logbert::DefaultLevels.each{|name, value| self.define_level(name, value)} end |
Instance Method Details
#[](x) ⇒ Object
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/logbert/levels.rb', line 72 def [](x) @quick_lookup[x] or begin if x.is_a? Integer # Return either the pre-defined level, or produce a virtual level. level = @value_to_level[x] || Logbert::Level.new("LEVEL_#{x}".to_sym, x) return level elsif x.is_a? String level = @name_to_level[x.to_sym] return level if level end raise KeyError, "No Level could be found for input: #{x}" end end |
#define_level(name, value) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/logbert/levels.rb', line 51 def define_level(name, value) unless name.instance_of?(Symbol) or name.instance_of?(String) raise ArgumentError, "The Level's name must be a Symbol or a String" end raise ArgumentError, "The Level's value must be an Integer" unless value.is_a? Integer # TODO: Verify that the name/value are not already taken raise KeyError, "A Level with that name is already defined: #{name}" if @name_to_level.has_key? name raise KeyError, "A Level with that value is already defined: #{value}" if @value_to_level.has_key? value level = Level.new(name, value) @name_to_level[name] = level @value_to_level[value] = level @quick_lookup[name] = @quick_lookup[value] = @quick_lookup[level] = level self.create_logging_method(name) self.create_predicate_method(name, value) end |
#levels ⇒ Object
46 47 48 |
# File 'lib/logbert/levels.rb', line 46 def levels @name_to_level.values end |
#names ⇒ Object
38 39 40 |
# File 'lib/logbert/levels.rb', line 38 def names @name_to_level.keys end |
#values ⇒ Object
42 43 44 |
# File 'lib/logbert/levels.rb', line 42 def values @value_to_level.keys end |