Class: Yell::Level
Overview
The Level
class handles the severities for you in order to determine if an adapter should log or not.
In order to setup your level, you have certain modifiers available:
at :warn # will be set to :warn level only
gt :warn # Will set from :error level onwards
gte :warn # Will set from :warn level onwards
lt :warn # Will set from :info level an below
lte :warn # Will set from :warn level and below
You are able to combine those modifiers to your convenience.
Constant Summary collapse
- InterpretRegexp =
/(at|gt|gte|lt|lte)?\.?(#{Yell::Severities.join('|')})/i
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #==(other) ⇒ Object
-
#at(*severities) ⇒ Yell::Level
Set the level at specific severities.
-
#at?(severity) ⇒ Boolean
Returns whether the level is allowed at the given severity.
-
#gt(severity) ⇒ Yell::Level
Set the level to greater than the given severity.
-
#gte(severity) ⇒ Yell::Level
Set the level greater or equal to the given severity.
-
#initialize(*severities) ⇒ Level
constructor
Create a new level instance.
-
#inspect ⇒ Object
Get a pretty string representation of the level, including the severities.
-
#lt(severity) ⇒ Yell::Level
Set the level lower than given severity.
-
#lte(severity) ⇒ Yell::Level
Set the level lower or equal than given severity.
-
#set(*severities) ⇒ Object
Set the severity to the given format.
- #severities ⇒ Object
-
#to_i ⇒ Object
(also: #to_int)
to_i implements backwards compatibility.
Constructor Details
#initialize(*severities) ⇒ Level
Create a new level instance.
45 46 47 |
# File 'lib/yell/level.rb', line 45 def initialize( *severities ) set(*severities) end |
Instance Method Details
#<=>(other) ⇒ Object
154 155 156 |
# File 'lib/yell/level.rb', line 154 def <=>( other ) other.is_a?(Numeric) ? to_i <=> other : super end |
#==(other) ⇒ Object
149 150 151 |
# File 'lib/yell/level.rb', line 149 def ==(other) other.respond_to?(:severities) ? severities == other.severities : super end |
#at(*severities) ⇒ Yell::Level
Set the level at specific severities
82 83 84 85 |
# File 'lib/yell/level.rb', line 82 def at( *severities ) severities.each { |severity| calculate! :==, severity } self end |
#at?(severity) ⇒ Boolean
Returns whether the level is allowed at the given severity
70 71 72 73 74 |
# File 'lib/yell/level.rb', line 70 def at?( severity ) index = index_from(severity) index.nil? ? false : @severities[index] end |
#gt(severity) ⇒ Yell::Level
Set the level to greater than the given severity
93 94 95 96 |
# File 'lib/yell/level.rb', line 93 def gt( severity ) calculate! :>, severity self end |
#gte(severity) ⇒ Yell::Level
Set the level greater or equal to the given severity
104 105 106 107 |
# File 'lib/yell/level.rb', line 104 def gte( severity ) calculate! :>=, severity self end |
#inspect ⇒ Object
Get a pretty string representation of the level, including the severities.
138 139 140 141 |
# File 'lib/yell/level.rb', line 138 def inspect inspectables = Yell::Severities.select.with_index { |l, i| !!@severities[i] } "#<#{self.class.name} severities: #{inspectables * ', '}>" end |
#lt(severity) ⇒ Yell::Level
Set the level lower than given severity
115 116 117 118 |
# File 'lib/yell/level.rb', line 115 def lt( severity ) calculate! :<, severity self end |
#lte(severity) ⇒ Yell::Level
Set the level lower or equal than given severity
126 127 128 129 |
# File 'lib/yell/level.rb', line 126 def lte( severity ) calculate! :<=, severity self end |
#set(*severities) ⇒ Object
Set the severity to the given format
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/yell/level.rb', line 50 def set( *severities ) @severities = Yell::Severities.map { true } severity = severities.length > 1 ? severities : severities.first case severity when Array then at(*severity) when Range then gte(severity.first).lte(severity.last) when String then interpret(severity) when Integer, Symbol then gte(severity) when Yell::Level then @severities = severity.severities end end |
#severities ⇒ Object
144 145 146 |
# File 'lib/yell/level.rb', line 144 def severities @severities end |
#to_i ⇒ Object Also known as: to_int
to_i implements backwards compatibility
132 133 134 |
# File 'lib/yell/level.rb', line 132 def to_i @severities.each_with_index { |s,i| return i if s == true } end |