Class: WarningSigns::Handler

Inherits:
Object
  • Object
show all
Defined in:
lib/warning_signs/handler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(behavior: nil, behaviors: [], environment: nil, except: [], only: [], source: "any", environments: [], ruby_warnings: {}, message_formatter: nil, message_formatters: []) ⇒ Handler

Returns a new instance of Handler.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/warning_signs/handler.rb', line 10

def initialize(
  behavior: nil,
  behaviors: [],
  environment: nil,
  except: [],
  only: [],
  source: "any",
  environments: [],
  ruby_warnings: {},
  message_formatter: nil,
  message_formatters: []
)
  @only_except = OnlyExcept.new(
    only: only.map { Pattern.for(_1) },
    except: except.map { Pattern.for(_1) }
  )
  @environments = environments.map { Environment.new(**_1.symbolize_keys) }
  if environment.present?
    @environments << Environment.new(
      environment: environment,
      behaviors: behaviors,
      behavior: behavior
    )
  end
  @source = source.to_s.downcase.inquiry
  @category_matcher = RubyCategoryMatcher.new(**ruby_warnings.symbolize_keys)
  @message_formatters = MessageFormatterList.new(
    message_formatters: message_formatters,
    message_formatter: message_formatter
  )
  raise InvalidHandlerError unless valid?
end

Instance Attribute Details

#backtrace_linesObject

Returns the value of attribute backtrace_lines.



3
4
5
# File 'lib/warning_signs/handler.rb', line 3

def backtrace_lines
  @backtrace_lines
end

#category_matcherObject

Returns the value of attribute category_matcher.



3
4
5
# File 'lib/warning_signs/handler.rb', line 3

def category_matcher
  @category_matcher
end

#environmentsObject

Returns the value of attribute environments.



3
4
5
# File 'lib/warning_signs/handler.rb', line 3

def environments
  @environments
end

#message_formattersObject

Returns the value of attribute message_formatters.



3
4
5
# File 'lib/warning_signs/handler.rb', line 3

def message_formatters
  @message_formatters
end

#only_exceptObject

Returns the value of attribute only_except.



3
4
5
# File 'lib/warning_signs/handler.rb', line 3

def only_except
  @only_except
end

#sourceObject

Returns the value of attribute source.



3
4
5
# File 'lib/warning_signs/handler.rb', line 3

def source
  @source
end

Class Method Details

.from_hash(hash) ⇒ Object



6
7
8
# File 'lib/warning_signs/handler.rb', line 6

def self.from_hash(hash)
  new(**hash.symbolize_keys)
end

Instance Method Details

#category_match?(category) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/warning_signs/handler.rb', line 66

def category_match?(category)
  category_matcher.match?(category)
end

#enabled_for_rails?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/warning_signs/handler.rb', line 79

def enabled_for_rails?
  source.rails? || source.all?
end

#enabled_for_ruby?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/warning_signs/handler.rb', line 75

def enabled_for_ruby?
  source.ruby? || source.all?
end

#environment(current: nil) ⇒ Object



83
84
85
86
87
88
# File 'lib/warning_signs/handler.rb', line 83

def environment(current: nil)
  current ||= Rails.env
  environments.find { _1.environment == current } ||
    environments.find { _1.environment.all? } ||
    environments.find { _1.environment.other? }
end

#known_source?(deprecation_source) ⇒ Boolean

Returns:

  • (Boolean)


52
53
54
# File 'lib/warning_signs/handler.rb', line 52

def known_source?(deprecation_source)
  deprecation_source.in?(%w[ruby rails])
end

#match?(deprecation) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
# File 'lib/warning_signs/handler.rb', line 60

def match?(deprecation)
  source_match?(deprecation.source) &&
    pattern_match?(deprecation.message) &&
    category_match?(deprecation.category)
end

#message_formatter_for(behavior) ⇒ Object



43
44
45
46
# File 'lib/warning_signs/handler.rb', line 43

def message_formatter_for(behavior)
  message_formatters.behavior_match(behavior) ||
    MessageFormatter::Text.new
end

#pattern_match?(message) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'lib/warning_signs/handler.rb', line 56

def pattern_match?(message)
  only_except.only_except_match?(message)
end

#source_match?(deprecation_source) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
# File 'lib/warning_signs/handler.rb', line 70

def source_match?(deprecation_source)
  return known_source?(deprecation_source) if source.any?
  source == deprecation_source
end

#valid?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/warning_signs/handler.rb', line 48

def valid?
  only_except.valid?
end