Class: Yell::Level

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/yell/level.rb

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.

Examples:

Set from :info to :error (including)

Yell::Level.new(:info).lte(:error)

Set from :info to :error (excluding)

Yell::Level.new(:info).lt(:error)

Set at :info only

Yell::Level.new.at(:info)

Constant Summary collapse

InterpretRegexp =
/(at|gt|gte|lt|lte)?\.?(#{Yell::Severities.join('|')})/i

Instance Method Summary collapse

Constructor Details

#initialize(*severities) ⇒ Level

Create a new level instance.

Examples:

Enable all severities

Yell::Level.new

Pass the minimum possible severity

Yell::Level.new :warn

Pass an array to exactly set the level at the given severities

Yell::Level.new [:info, :error]

Pass a range to set the level within the severities

Yell::Level.new (:info..:error)

Parameters:

  • severity (Integer, String, Symbol, Array, Range, nil)

    The severity for the level.



43
44
45
46
# File 'lib/yell/level.rb', line 43

def initialize( *severities )
  @tainted = false
  set(*severities)
end

Instance Method Details

#<=>(other) ⇒ Object



153
154
155
# File 'lib/yell/level.rb', line 153

def <=>( other )
  other.is_a?(Numeric) ? to_i <=> other : super
end

#==(other) ⇒ Object



148
149
150
# File 'lib/yell/level.rb', line 148

def ==(other)
  other.respond_to?(:severities) ? severities == other.severities : super
end

#at(*severities) ⇒ Yell::Level

Set the level at specific severities

Examples:

Set at :debug and :error only

at :debug, :error

Returns:



81
82
83
84
# File 'lib/yell/level.rb', line 81

def at( *severities )
  severities.each { |severity| calculate! :==, severity }
  self
end

#at?(severity) ⇒ Boolean

Returns whether the level is allowed at the given severity

Examples:

at? :warn
at? 0       # debug

Returns:

  • (Boolean)

    tru or false



69
70
71
72
73
# File 'lib/yell/level.rb', line 69

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

Examples:

Set to :error and above

gt :warn

Returns:



92
93
94
95
# File 'lib/yell/level.rb', line 92

def gt( severity )
  calculate! :>, severity
  self
end

#gte(severity) ⇒ Yell::Level

Set the level greater or equal to the given severity

Examples:

Set to :warn and above

gte :warn

Returns:



103
104
105
106
# File 'lib/yell/level.rb', line 103

def gte( severity )
  calculate! :>=, severity
  self
end

#inspectObject

Get a pretty string representation of the level, including the severities.



137
138
139
140
# 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

#lt(severity) ⇒ Yell::Level

Set the level lower than given severity

Examples:

Set to lower than :warn

lt :warn

Returns:



114
115
116
117
# File 'lib/yell/level.rb', line 114

def lt( severity )
  calculate! :<, severity
  self
end

#lte(severity) ⇒ Yell::Level

Set the level lower or equal than given severity

Examples:

Set to lower or equal than :warn

lte :warn

Returns:



125
126
127
128
# File 'lib/yell/level.rb', line 125

def lte( severity )
  calculate! :<=, severity
  self
end

#set(*severities) ⇒ Object

Set the severity to the given format



49
50
51
52
53
54
55
56
57
58
59
60
# 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

#severitiesObject



143
144
145
# File 'lib/yell/level.rb', line 143

def severities
  @severities
end

#to_iObject Also known as: to_int

to_i implements backwards compatibility



131
132
133
# File 'lib/yell/level.rb', line 131

def to_i
  @severities.each_with_index { |s,i| return i if s == true }
end