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

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.



61
62
63
64
65
66
67
68
69
# File 'lib/yell/level.rb', line 61

def initialize( severity = nil )
  reset!

  case severity
    when Array then at( *severity )
    when Range then gte(severity.first).lte(severity.last)
    when Integer, String, Symbol then gte(severity)
  end
end

Instance Attribute Details

#severitiesObject (readonly)

Returns the value of attribute severities.



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

def severities
  @severities
end

Instance Method Details

#at(*severities) ⇒ Object

:nodoc:



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

def at( *severities ) #:nodoc:
  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)


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

def at?( severity )
  index = index_from( severity )

  index.nil? ? false : @severities[index]
end

#gt(severity) ⇒ Object

:nodoc:



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

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

#gte(severity) ⇒ Object

:nodoc:



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

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

#lt(severity) ⇒ Object

:nodoc:



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

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

#lte(severity) ⇒ Object

:nodoc:



112
113
114
115
# File 'lib/yell/level.rb', line 112

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

#reset!Object



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

def reset!
  @severities = Yell::Severities.map { true }
end

#to_iObject Also known as: to_int

to_i implements backwards compatibility



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

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