class YardJunk::Logger::Message

Constants

DEFAULT_FORMAT

Attributes

extra[R]
file[R]
line[R]
message[R]
severity[R]

Public Class Methods

new(message:, severity: :warn, code_object: nil, file: nil, line: nil, **extra) click to toggle source
# 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
pattern(regexp) click to toggle source
# File lib/yard-junk/logger/message.rb, line 53
def pattern(regexp)
  @pattern = regexp
  Message.registry << self
end
registry() click to toggle source
# File lib/yard-junk/logger/message.rb, line 49
def registry
  @registry ||= []
end
search_up(pattern) click to toggle source
# File lib/yard-junk/logger/message.rb, line 58
def search_up(pattern) # rubocop:disable Style/TrivialAccessors
  @search_up = pattern
end
try_parse(line, **context) click to toggle source
# 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
type() click to toggle source
# File lib/yard-junk/logger/message.rb, line 71
def type
  !name || name.end_with?('::Message') ? 'UnknownError' : name.sub(/^.+::/, '')
end
valid_type?(type) click to toggle source
# File lib/yard-junk/logger/message.rb, line 75
def valid_type?(type)
  type == 'UnknownError' || registry.any? { |m| m.type == type }
end

Private Class Methods

find_object(file, line) click to toggle source
# File lib/yard-junk/logger/message.rb, line 97
def find_object(file, line)
  YARD::Registry.detect { |o| o.file == file && o.line == line }
end
guard_line(data) click to toggle source
# File lib/yard-junk/logger/message.rb, line 81
def guard_line(data) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/CyclomaticComplexity,Metrics/PerceivedComplexity
  # FIXME: Ugly, huh?
  data[:file] && data[:line] && @search_up or return data
  data = data.merge(line: data[:line].to_i)
  data = data.merge(code_object: find_object(data[:file], data[:line]))
  lines = File.readlines(data[:file]) rescue (return data) # rubocop:disable Style/RescueModifier
  pattern = Regexp.new(@search_up % data.transform_values { |v| Regexp.escape(v.to_s) })
  _, num = lines.map
                .with_index { |ln, i| [ln, i + 1] }
                .first(data[:line]).reverse
                .detect { |ln,| pattern.match(ln) }
  num or return data

  data.merge(line: num)
end

Public Instance Methods

==(other) click to toggle source
# File lib/yard-junk/logger/message.rb, line 32
def ==(other)
  other.is_a?(self.class) && to_h == other.to_h
end
to_h() click to toggle source
# 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) click to toggle source
# File lib/yard-junk/logger/message.rb, line 38
def to_s(format = DEFAULT_FORMAT)
  format % to_h
end
type() click to toggle source
# File lib/yard-junk/logger/message.rb, line 42
def type
  self.class.type
end