Module: Pymn::ChainOfResponsibility::ClassMethods

Defined in:
lib/pymn/chain_of_responsibility.rb

Instance Method Summary collapse

Instance Method Details

#responsibility(method, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/pymn/chain_of_responsibility.rb', line 16

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