Class: WarningSigns::MessageFormatter::Base

Inherits:
Object
  • Object
show all
Includes:
CallerHelper
Defined in:
lib/warning_signs/message_formatter/base.rb

Direct Known Subclasses

Hash, Json, Text, Yaml

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from CallerHelper

#caller_filtered, #ignore_line

Constructor Details

#initialize(backtrace_lines: 0, behaviors: {}, environments: {}, filter_backtraces: "yes") ⇒ Base

Returns a new instance of Base.



14
15
16
17
18
19
# File 'lib/warning_signs/message_formatter/base.rb', line 14

def initialize(backtrace_lines: 0, behaviors: {}, environments: {}, filter_backtraces: "yes")
  @backtrace_lines = backtrace_lines
  @behaviors = OnlyExcept.new(**behaviors.symbolize_keys)
  @environments = OnlyExcept.new(**environments.symbolize_keys)
  @filter_backtraces = filter_backtraces.to_s.downcase.inquiry
end

Instance Attribute Details

#backtrace_linesObject (readonly)

Returns the value of attribute backtrace_lines.



5
6
7
# File 'lib/warning_signs/message_formatter/base.rb', line 5

def backtrace_lines
  @backtrace_lines
end

#behaviorsObject (readonly)

Returns the value of attribute behaviors.



5
6
7
# File 'lib/warning_signs/message_formatter/base.rb', line 5

def behaviors
  @behaviors
end

#environmentsObject (readonly)

Returns the value of attribute environments.



5
6
7
# File 'lib/warning_signs/message_formatter/base.rb', line 5

def environments
  @environments
end

#filter_backtracesObject (readonly)

Returns the value of attribute filter_backtraces.



5
6
7
# File 'lib/warning_signs/message_formatter/base.rb', line 5

def filter_backtraces
  @filter_backtraces
end

Class Method Details

.for(**args) ⇒ Object



7
8
9
10
11
12
# File 'lib/warning_signs/message_formatter/base.rb', line 7

def self.for(**args)
  args = args.symbolize_keys
  type = args.delete(:format) || "text"
  class_name = "WarningSigns::MessageFormatter::#{type.classify}"
  class_name.constantize.new(**args)
end

Instance Method Details

#filtered_backtrace(backtrace) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/warning_signs/message_formatter/base.rb', line 25

def filtered_backtrace(backtrace)
  return [] if backtrace.nil?
  result = backtrace.reject do |location|
    ignore_line(location.to_s, filter_backtraces: filter_backtraces)
  end
  result.empty? ? backtrace : result
end

#filtered_backtrace_lines(backtrace) ⇒ Object



33
34
35
36
# File 'lib/warning_signs/message_formatter/base.rb', line 33

def filtered_backtrace_lines(backtrace)
  return [] if backtrace_lines.zero?
  filtered_backtrace(backtrace)[1..backtrace_lines] || []
end

#format_message(message, backtrace) ⇒ Object

Raises:

  • (NotImplementedError)


21
22
23
# File 'lib/warning_signs/message_formatter/base.rb', line 21

def format_message(message, backtrace)
  raise NotImplementedError
end