lib/rapporteur/checker.rb in rapporteur-2.1.0 vs lib/rapporteur/checker.rb in rapporteur-3.0.0

- old
+ new

@@ -1,20 +1,18 @@ -require 'set' - 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 - include ActiveModel::Validations extend CheckerDeprecations def initialize - @messages = Hash.new - @checks = Set.new + @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 @@ -34,13 +32,13 @@ # Returns self. # Raises ArgumentError if the given check does not respond to call. # def add_check(object_or_nil_with_block=nil, &block) if block_given? - @checks << block + @check_list.add(block) elsif object_or_nil_with_block.respond_to?(:call) - @checks << object_or_nil_with_block + @check_list.add(object_or_nil_with_block) else raise ArgumentError, "A check must respond to #call." end self end @@ -51,11 +49,11 @@ # start from scratch. # # Returns self. # def clear - @checks.clear + @check_list.clear self end ## # Public: Checks can call this method to halt any further processing. This @@ -76,11 +74,11 @@ # # Returns self. # def run reset - @checks.each do |object| + @check_list.each do |object| object.call(self) break if @halted end self end @@ -104,13 +102,11 @@ # i18n_key_is_better: 'Look, localization!' # # Returns self. # def add_error(key, message, options={}) - options[:scope] = [:rapporteur, :errors, key] - options[:default] = [message, message.to_s.humanize] - errors.add(key, message, options) + @errors.add(key, message) self end ## # Public: Adds a status message for inclusion in the success response. @@ -126,22 +122,29 @@ # checker.add_message(:load, 0.934) # # Returns self. # def add_message(name, message) - @messages[name] = message + @messages.add(name, message) self end ## # Internal: Returns a hash of messages suitable for conversion into JSON. # def as_json(args={}) - @messages + @messages.to_hash end ## + # Internal: Used by Rails' JSON serialization to render error messages. + # + def errors + @errors + end + + ## # Internal: Used by Rails' JSON serialization, specifically in # ActionController::Responder. # def read_attribute_for_serialization(key) @messages[key] @@ -154,9 +157,9 @@ def reset @halted = false @messages.clear - errors.clear + @errors.clear end end end