This is the gem for building nice looking APIs where you want to delegate method calls to another object at a later time.
See Parallizer for an example.
gem install hanging_methods
Here's an example.
require 'hanging_methods'
class Interesting
include HangingMethods
add_hanging_method :add
end
interesting = Interesting.new
interesting.add.a_method
interesting.add.another_method(1, 2)
puts interesting.hanging_method_invocations(:add)
# [[:a_method], [:another_method, 1, 2]]
You can be notified of method being hanged and specify its result.
require 'hanging_methods'
class Interesting
include HangingMethods
add_hanging_method :add, after_invocation: :added
private
def added(method_name, arg_1, arg_2, &block)
return "very_interesting: #{arg_1}, #{arg_2}, #{yield}"
end
end
interesting = Interesting.new
interesting.add.a_method(1, 2) { 3 }
# returns "very_interesting: 1, 2, 3"
Hanging Methods is maintained by Michael Pearce
Copyright (c) 2013 Michael Pearce. See LICENSE.txt for further details.