Class: YardJunk::Logger::Message

Inherits:
Object
  • Object
show all
Includes:
Spellcheck
Defined in:
lib/yard-junk/logger/message.rb

Constant Summary collapse

DEFAULT_FORMAT =
'%{file}:%{line}: [%{type}] %{message}'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Spellcheck

#spell_check

Constructor Details

#initialize(message:, severity: :warn, code_object: nil, file: nil, line: nil, **extra) ⇒ Message

Returns a new instance of Message.



10
11
12
13
14
15
16
17
# File 'lib/yard-junk/logger/message.rb', line 10

def initialize(message:, severity: :warn, code_object: nil, file: nil, line: nil, **extra)
  @message = message.gsub(/\s{2,}/, ' ')
  @file = file
  @line = line&.to_i
  @code_object = code_object
  @severity = severity
  @extra = extra
end

Instance Attribute Details

#extraObject (readonly)

Returns the value of attribute extra.



8
9
10
# File 'lib/yard-junk/logger/message.rb', line 8

def extra
  @extra
end

#fileObject (readonly)

Returns the value of attribute file.



8
9
10
# File 'lib/yard-junk/logger/message.rb', line 8

def file
  @file
end

#lineObject (readonly)

Returns the value of attribute line.



8
9
10
# File 'lib/yard-junk/logger/message.rb', line 8

def line
  @line
end

#messageObject (readonly)

Returns the value of attribute message.



8
9
10
# File 'lib/yard-junk/logger/message.rb', line 8

def message
  @message
end

#severityObject (readonly)

Returns the value of attribute severity.



8
9
10
# File 'lib/yard-junk/logger/message.rb', line 8

def severity
  @severity
end

Class Method Details

.pattern(regexp) ⇒ Object



53
54
55
56
# File 'lib/yard-junk/logger/message.rb', line 53

def pattern(regexp)
  @pattern = regexp
  Message.registry << self
end

.registryObject



49
50
51
# File 'lib/yard-junk/logger/message.rb', line 49

def registry
  @registry ||= []
end

.search_up(pattern) ⇒ Object



58
59
60
# File 'lib/yard-junk/logger/message.rb', line 58

def search_up(pattern)
  @search_up = pattern
end

.try_parse(line, **context) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/yard-junk/logger/message.rb', line 62

def try_parse(line, **context)
  @pattern or fail StandardError, "Pattern is not defined for #{self}"
  match = @pattern.match(line) or return nil
  data = context.compact
                .merge(match.names.map(&:to_sym).zip(match.captures).to_h.compact)
  data = guard_line(data)
  new(**data)
end

.typeObject



71
72
73
# File 'lib/yard-junk/logger/message.rb', line 71

def type
  !name || name.end_with?('::Message') ? 'UnknownError' : name.sub(/^.+::/, '')
end

.valid_type?(type) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/yard-junk/logger/message.rb', line 75

def valid_type?(type)
  type == 'UnknownError' || registry.any? { |m| m.type == type }
end

Instance Method Details

#==(other) ⇒ Object



32
33
34
# File 'lib/yard-junk/logger/message.rb', line 32

def ==(other)
  other.is_a?(self.class) && to_h == other.to_h
end

#to_hObject



23
24
25
26
27
28
29
30
# File 'lib/yard-junk/logger/message.rb', line 23

def to_h
  {
    type: type,
    message: message,
    file: file,
    line: line&.to_i || 1
  }.merge(extra)
end

#to_s(format = DEFAULT_FORMAT) ⇒ Object



38
39
40
# File 'lib/yard-junk/logger/message.rb', line 38

def to_s(format = DEFAULT_FORMAT)
  format % to_h
end

#typeObject



42
43
44
# File 'lib/yard-junk/logger/message.rb', line 42

def type
  self.class.type
end