lib/rapporteur/checker.rb in rapporteur-3.6.0 vs lib/rapporteur/checker.rb in rapporteur-3.6.1
- old
+ new
@@ -1,22 +1,20 @@
+# frozen_string_literal: true
+
module Rapporteur
# The center of the Rapporteur library, Checker manages holding and running
# the custom checks, holding any application error messages, and provides the
# controller with that data for rendering.
#
class Checker
extend CheckerDeprecations
-
def initialize
- @messages = MessageList.new(:messages)
- @errors = MessageList.new(:errors)
@check_list = CheckList.new
reset
end
-
# Public: Add a pre-built or custom check to your status endpoint. These
# checks are used to test the state of the world of the application, and
# need only respond to `#call`.
#
# Once added, the given check will be called and passed an instance of this
@@ -30,17 +28,17 @@
# }
#
# Returns self.
# Raises ArgumentError if the given check does not respond to call.
#
- def add_check(object_or_nil_with_block=nil, &block)
+ def add_check(object_or_nil_with_block = nil, &block)
if block_given?
- @check_list.add(block)
+ check_list.add(block)
elsif object_or_nil_with_block.respond_to?(:call)
- @check_list.add(object_or_nil_with_block)
+ check_list.add(object_or_nil_with_block)
else
- raise ArgumentError, "A check must respond to #call."
+ raise ArgumentError, 'A check must respond to #call.'
end
self
end
# Public: Empties all configured checks from the checker. This may be
@@ -49,11 +47,11 @@
# start from scratch.
#
# Returns self.
#
def clear
- @check_list.clear
+ check_list.clear
self
end
##
# Public: Checks can call this method to halt any further processing. This
@@ -74,11 +72,11 @@
#
# Returns self.
#
def run
reset
- @check_list.each do |object|
+ check_list.each do |object|
object.call(self)
break if @halted
end
self
end
@@ -114,12 +112,12 @@
# i18n_key_is_better: 'Look, localization!'
# i18n_with_variable: 'Look, %{custom_variable}!'
#
# Returns self.
#
- def add_error(name, message, i18n_options={})
- @errors.add(name, message, i18n_options)
+ def add_error(name, message, i18n_options = {})
+ errors.add(name, message, i18n_options)
self
end
##
# Public: Adds a status message for inclusion in the success response.
@@ -141,44 +139,48 @@
# checker.add_message(:load, 0.934)
# checker.add_message(:load, :too_high, :measurement => '0.934')
#
# Returns self.
#
- def add_message(name, message, i18n_options={})
- @messages.add(name, message, i18n_options)
+ def add_message(name, message, i18n_options = {})
+ messages.add(name, message, i18n_options)
self
end
##
# Internal: Returns a hash of messages suitable for conversion into JSON.
#
- def as_json(args={})
- @messages.to_hash
+ def as_json(_args = {})
+ messages.to_hash
end
##
# Internal: Used by Rails' JSON serialization to render error messages.
#
def errors
- @errors
+ Thread.current[:rapporteur_errors] ||= MessageList.new(:errors)
end
##
# Internal: Used by Rails' JSON serialization.
#
def read_attribute_for_serialization(key)
- @messages[key]
+ messages[key]
end
alias read_attribute_for_validation read_attribute_for_serialization
-
private
+ attr_reader :check_list
+ def messages
+ Thread.current[:rapporteur_messages] ||= MessageList.new(:messages)
+ end
+
def reset
@halted = false
- @messages.clear
- @errors.clear
+ messages.clear
+ errors.clear
end
end
end