Class: Smplkit::LogLevel

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/smplkit/log_level.rb

Overview

Log severity levels used by the Smpl Logging service.

Acts as a string-valued enum: each constant equals its name when used in string contexts, and supports comparison via the ordinal.

Constant Summary collapse

NAMES =
%w[TRACE DEBUG INFO WARN ERROR FATAL SILENT].freeze
TRACE =
new("TRACE", 0)
DEBUG =
new("DEBUG", 1)
INFO =
new("INFO", 2)
WARN =
new("WARN", 3)
ERROR =
new("ERROR", 4)
FATAL =
new("FATAL", 5)
SILENT =
new("SILENT", 6)
ALL =
[TRACE, DEBUG, INFO, WARN, ERROR, FATAL, SILENT].freeze
BY_NAME =
ALL.to_h { |lvl| [lvl.name, lvl] }.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, ordinal) ⇒ LogLevel

Returns a new instance of LogLevel.



13
14
15
16
17
# File 'lib/smplkit/log_level.rb', line 13

def initialize(name, ordinal)
  @name = name.freeze
  @ordinal = ordinal
  freeze
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/smplkit/log_level.rb', line 11

def name
  @name
end

#ordinalObject (readonly)

Returns the value of attribute ordinal.



11
12
13
# File 'lib/smplkit/log_level.rb', line 11

def ordinal
  @ordinal
end

Class Method Details

.coerce(value) ⇒ Object



51
52
53
54
55
# File 'lib/smplkit/log_level.rb', line 51

def self.coerce(value)
  return value if value.is_a?(LogLevel)

  from_string(value)
end

.from_string(value) ⇒ Object

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
# File 'lib/smplkit/log_level.rb', line 41

def self.from_string(value)
  raise ArgumentError, "log level cannot be nil" if value.nil?

  key = value.to_s.upcase
  level = BY_NAME[key]
  raise ArgumentError, "unknown log level: #{value.inspect}" unless level

  level
end

Instance Method Details

#<=>(other) ⇒ Object



25
# File 'lib/smplkit/log_level.rb', line 25

def <=>(other) = other.is_a?(LogLevel) ? @ordinal <=> other.ordinal : nil

#==(other) ⇒ Object



22
# File 'lib/smplkit/log_level.rb', line 22

def ==(other) = other.is_a?(LogLevel) ? @ordinal == other.ordinal : @name == other

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


24
# File 'lib/smplkit/log_level.rb', line 24

def eql?(other) = self == other

#hashObject



23
# File 'lib/smplkit/log_level.rb', line 23

def hash = @ordinal.hash

#inspectObject



21
# File 'lib/smplkit/log_level.rb', line 21

def inspect = "#<Smplkit::LogLevel #{@name}>"

#to_sObject



19
# File 'lib/smplkit/log_level.rb', line 19

def to_s = @name

#to_strObject



20
# File 'lib/smplkit/log_level.rb', line 20

def to_str = @name