Sha256: 9be868d1d5114a9e69fa6e79593bfc392eb9a38831515a622502566274b906f9

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

# frozen_string_literal: true

require_relative 'checker'

module Checkability
  # @abstract
  class BaseChecker < Checker
    # @return [Handler]
    attr_reader :stop_process_on_success, :stop_process_on_failure,
                :success_message, :failure_message

    def initialize(opts = {})
      @stop_process_on_failure = opts[:stop_process_on_failure] || false
      @stop_process_on_success = opts[:stop_process_on_success] || false
      @success_message = opts[:success_message] || 'Success.'
      @failure_message = opts[:failure_message] || 'Failed.'

      @next_handler = nil
      post_initialize(opts) # implemented in subclass
    end

    # subclass should implement
    def post_initialize(_opts)
      nil
    end

    # @param [Handler] handler
    #
    # @return [Handler]
    def next_handler(handler)
      @next_handler = handler

      handler
    end

    # @abstract
    #
    # @param [String] request
    #
    # @return [Boolean, nil]
    def handle(check_obj)
      res, mess = result_and_message(check_obj)
      check_obj.ch_messages << mess
      check_obj.ch_allowed = res

      return if _stop_here?(res)

      @next_handler&.handle(check_obj) if @next_handler
    end

    def result_and_message(check_obj = nil)
      res = result(check_obj)

      str = res ? success_message : failure_message
      [res, message(res, str)]
    rescue StandardError => e
      [false, message(false, e)]
    end

    # subclass should implement
    def result(_check_obj)
      raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
    end

    # subclass may override
    def message(res, str)
      "#{res}:::#{str}"
    end

    private

    def _stop_here?(res)
      (res && stop_process_on_success) || (!res && stop_process_on_failure)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
checkability-2.1.0 lib/checkability/base_checker.rb
checkability-2.0.3 lib/checkability/base_checker.rb
checkability-2.0.2 lib/checkability/base_checker.rb
checkability-2.0.1 lib/checkability/base_checker.rb