class Checkability::BaseChecker

@abstract

Attributes

failure_message[R]

@return [Handler]

stop_process_on_failure[R]

@return [Handler]

stop_process_on_success[R]

@return [Handler]

success_message[R]

@return [Handler]

Public Class Methods

new(opts = {}) click to toggle source
# File lib/checkability/base_checker.rb, line 12
def initialize(opts = {})
  @stop_process_on_failure = opts.fetch(:stop_process_on_failure) { false }
  @stop_process_on_success = opts.fetch(:stop_process_on_success) { false }
  @success_message = opts.fetch(:success_message) { 'Success.' }
  @failure_message = opts.fetch(:failure_message) { 'Failed.' }

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

Public Instance Methods

handle(check_obj) click to toggle source

@abstract

@param [String] request

@return [Boolean, nil]

# File lib/checkability/base_checker.rb, line 41
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
message(res, str) click to toggle source

subclass may override

# File lib/checkability/base_checker.rb, line 66
def message(res, str)
  "#{res}:::#{str}"
end
next_handler(handler) click to toggle source

@param [Handler] handler

@return [Handler]

# File lib/checkability/base_checker.rb, line 30
def next_handler(handler)
  @next_handler = handler

  handler
end
post_initialize(_opts) click to toggle source

subclass should implement

# File lib/checkability/base_checker.rb, line 23
def post_initialize(_opts)
  nil
end
result(_check_obj) click to toggle source

subclass should implement

# File lib/checkability/base_checker.rb, line 61
def result(_check_obj)
  raise NotImplementedError, "#{self.class} has not implemented method '#{__method__}'"
end
result_and_message(check_obj = nil) click to toggle source
# File lib/checkability/base_checker.rb, line 51
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

Private Instance Methods

_stop_here?(res) click to toggle source
# File lib/checkability/base_checker.rb, line 72
def _stop_here?(res)
  (res && stop_process_on_success) || (!res && stop_process_on_failure)
end