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.
@example Set from :info to :error (including)
Yell::Level.new(:info).lte(:error)
@example Set from :info to :error (excluding)
Yell::Level.new(:info).lt(:error)
@example Set at :info only
Yell::Level.new.at(:info)
Create a new level instance.
@example Enable all severities
Yell::Level.new
@example Pass the minimum possible severity
Yell::Level.new :warn
@example Pass an array to exactly set the level at the given severities
Yell::Level.new [:info, :error]
@example Pass a range to set the level within the severities
Yell::Level.new (:info..:error)
@param [Integer,String,Symbol,Array,Range,nil] severity The severity for the level.
# File lib/yell/level.rb, line 44 def initialize( *severities ) set(*severities) end
@private
# File lib/yell/level.rb, line 153 def <=>( other ) other.is_a?(Numeric) ? to_i <=> other : super end
@private
# File lib/yell/level.rb, line 148 def ==(other) other.respond_to?(:severities) ? severities == other.severities : super end
Set the level at specific severities
@example Set at :debug and :error only
at :debug, :error
@return [Yell::Level] the instance
# File lib/yell/level.rb, line 81 def at( *severities ) severities.each { |severity| calculate! :==, severity } self end
Returns whether the level is allowed at the given severity
@example
at? :warn at? 0 # debug
@return [Boolean] tru or false
# File lib/yell/level.rb, line 69 def at?( severity ) index = index_from(severity) index.nil? ? false : @severities[index] end
Set the level to greater than the given severity
@example Set to :error and above
gt :warn
@return [Yell::Level] the instance
# File lib/yell/level.rb, line 92 def gt( severity ) calculate! :>, severity self end
Set the level greater or equal to the given severity
@example Set to :warn and above
gte :warn
@return [Yell::Level] the instance
# File lib/yell/level.rb, line 103 def gte( severity ) calculate! :>=, severity self end
Get a pretty string representation of the level, including the severities.
# File lib/yell/level.rb, line 137 def inspect inspectables = Yell::Severities.select.with_index { |l, i| !!@severities[i] } "#<#{self.class.name} severities: #{inspectables * ', '}>" end
Set the level lower than given severity
@example Set to lower than :warn
lt :warn
@return [Yell::Level] the instance
# File lib/yell/level.rb, line 114 def lt( severity ) calculate! :<, severity self end
Set the level lower or equal than given severity
@example Set to lower or equal than :warn
lte :warn
@return [Yell::Level] the instance
# File lib/yell/level.rb, line 125 def lte( severity ) calculate! :<=, severity self end
Set the severity to the given format
# File lib/yell/level.rb, line 49 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
@private
# File lib/yell/level.rb, line 143 def severities @severities end
# File lib/yell/level.rb, line 190 def ascending!( index ) each { |s, i| @severities[i] = i < index ? false : true } end
# File lib/yell/level.rb, line 168 def calculate!( modifier, severity ) index = index_from(severity) return if index.nil? case modifier when :> then ascending!( index+1 ) when :>= then ascending!( index ) when :< then descending!( index-1 ) when :<= then descending!( index ) else set!( index ) # :== end taint unless tainted? end
# File lib/yell/level.rb, line 194 def descending!( index ) each { |s, i| @severities[i] = index < i ? false : true } end
# File lib/yell/level.rb, line 198 def each @severities.each_with_index do |s, i| next if s == false # skip yield(s, i) end end
# File lib/yell/level.rb, line 183 def index_from( severity ) case severity when String, Symbol then Yell::Severities.index(severity.to_s.upcase) else Integer(severity) end end
# File lib/yell/level.rb, line 160 def interpret( severities ) severities.split( ' ' ).each do |severity| if m = InterpretRegexp.match(severity) m[1].nil? ? __send__( :gte, m[2] ) : __send__( m[1], m[2] ) end end end
# File lib/yell/level.rb, line 206 def set!( index, val = true ) @severities.map! { false } unless tainted? @severities[index] = val end