Class: Yell::Level

Inherits:
Object
  • Object
show all
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)

Defined Under Namespace

Modules: Helpers

Constant Summary collapse

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(severity = nil) ⇒ 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) (defaults to: nil)

    The severity for the level.



63
64
65
# File 'lib/yell/level.rb', line 63

def initialize( severity = nil )
  reset!( severity )
end

Instance Attribute Details

#severitiesObject (readonly)

Returns the value of attribute severities.



46
47
48
# File 'lib/yell/level.rb', line 46

def severities
  @severities
end

Instance Method Details

#at(*severities) ⇒ Yell::Level

Set the level at specific severities.

Examples:

Set at :debug and :error only

at :debug, :error

Returns:



86
87
88
89
# File 'lib/yell/level.rb', line 86

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



74
75
76
77
78
# File 'lib/yell/level.rb', line 74

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:



97
98
99
100
# File 'lib/yell/level.rb', line 97

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:



108
109
110
111
# File 'lib/yell/level.rb', line 108

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

#inspectString

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

Examples:

Inspect the level.

level.inspect

Returns:

  • (String)

    The inspection string.



148
149
150
151
152
153
154
155
# File 'lib/yell/level.rb', line 148

def inspect
  severities = Yell::Severities.each.with_index.inject( [] ) do |r, (l, i)|
    r << l if @severities[i]
    r
  end

  "#<#{self.class.name} severities: #{severities * ', '}>"
end

#lt(severity) ⇒ Yell::Level

Set the level lower than given severity

Examples:

Set to lower than :warn

lt :warn

Returns:



119
120
121
122
# File 'lib/yell/level.rb', line 119

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:



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

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

#to_iObject Also known as: to_int

to_i implements backwards compatibility



136
137
138
# File 'lib/yell/level.rb', line 136

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