Class: Handoff::VerifyingDelegate

Inherits:
Object
  • Object
show all
Defined in:
lib/handoff/verifying_delegate.rb

Instance Method Summary collapse

Constructor Details

#initialize(target_method, expected_args) ⇒ VerifyingDelegate

Returns a new instance of VerifyingDelegate.



3
4
5
6
7
8
9
10
11
12
# File 'lib/handoff/verifying_delegate.rb', line 3

def initialize target_method, expected_args
  @target_method = target_method
  (class << self; self; end).class_eval do
    define_method target_method do |*actual_args|
      raise 'called twice' if @returned
      raise( ArgumentError, "expected arguments: '#{expected_args}' but got '#{actual_args}'") unless expected_args == actual_args
      @returned = true and happy_return
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

Raises:

  • (NoMethodError)


18
19
20
# File 'lib/handoff/verifying_delegate.rb', line 18

def method_missing method, *args
  raise NoMethodError, "Expected handoff to method `#{@target_method}', but `#{method}' was called"
end

Instance Method Details

#happy_returnObject



14
15
16
# File 'lib/handoff/verifying_delegate.rb', line 14

def happy_return
  "handoff return value from #{to_s}"
end