Sha256: 0d1e912fd3af72338fdb0f2140356c32474027e3fe0519e272540f7f3cba12a7

Contents?: true

Size: 1.42 KB

Versions: 1

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

require 'forwardable'

module Checkability
  # Implements check method to Iterate on chechers
  # Possible to implemet as Iterator in future
  #
  class Checkable
    attr_accessor :check_obj, :handler_confs

    extend Forwardable
    def_delegators :@check_obj, :ch_messages, :ch_allowed, :ch_conf

    def initialize(check_obj)
      @check_obj = check_obj
    end

    # As in result handlers should behave like this:
    # validator    .next_handler(storage)
    # storage      .next_handler(api_validator)
    # api_validator.next_handler(api_finder)
    # api_validator.next_handler(nil)
    #
    # validator.handle(request)
    #
    # ChainOfResponsibilty
    #
    def check
      _first_handler.handle(check_obj)
    rescue StandardError => e
      check_obj.ch_messages << "false:::#{e}: #{handler_confs}."
      false
    end

    private

    def _first_handler
      _handlers[ch_conf.keys.first]
    end

    def _handlers
      handlers = _make_handlers
      handlers.each_value.with_index do |handler, i|
        next_handler_name = handlers.keys[i + 1]
        handler.next_handler(handlers[next_handler_name]) if handlers[next_handler_name]
      end
      handlers
    end

    def _make_handlers
      ch_conf.transform_values { |conf| _make_handler(conf) }
    end

    def _make_handler(conf)
      Checkability.const_get(conf[:name].to_s.camelize)
                  .new(conf)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
checkability-2.0.3 lib/checkability/checkable.rb