Sha256: aa82912ac74032ca24ac9000eb4ceb594d2413977b7dc7d74851ef1f9ce5447c

Contents?: true

Size: 986 Bytes

Versions: 2

Compression:

Stored size: 986 Bytes

Contents

require 'active_support/concern'
require 'pymn/chain_of_responsibility/command_not_handled_error'
require 'pymn/chain_of_responsibility/responsibility_method_undefined_error'
require 'pymn/chain_of_responsibility/has_handlers'

module Pymn
  module ChainOfResponsibility
    extend ActiveSupport::Concern

    included do
      include HasHandlers
    end

    module ClassMethods

      def responsibility method, &block
        unless method_defined?(method)
          raise ResponsibilityMethodUndefinedError.new(method)
        end

        command_method = "__command_#{method}".to_sym
        alias_method command_method, method

        define_method(method) do |*args|
          if instance_exec(*args, &block)
            return send(command_method, *args)
          end

          if @next_handler_in_chain
            return @next_handler_in_chain.send(method, *args)
          end

          raise CommandNotHandledError.new(method)
        end
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
pymn-0.0.2 lib/pymn/chain_of_responsibility.rb
pymn-0.0.1 lib/pymn/chain_of_responsibility.rb