-
-
Save sorah/9263951 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo | |
def initialize | |
@hooks = [] | |
@methods = {} | |
@existing_methods = self.methods | |
end | |
def add(mode=nil, &block) | |
@hooks << block | |
end | |
def hola | |
p :hola | |
end | |
def to_proc(methods={}) | |
hooks = @hooks | |
methods.each do |name, meth| | |
if @existing_methods.include?(name) | |
raise ArgumentError, "conflicts with existing method" | |
end | |
unless self.respond_to?(name) | |
self.define_singleton_method(name) { |*args, &block| | |
(@methods[Thread.current] ||= {})[name].call(self, *args, &block) | |
} | |
end | |
end | |
this = self | |
proc do |*args| | |
begin | |
methods.each do |name, meth| | |
(@methods[Thread.current] ||= {})[name] = meth | |
end | |
hooks.each do |hook| | |
this.instance_eval(*args, &hook) | |
end | |
ensure | |
@methods.delete Thread.current | |
end | |
end | |
end | |
end | |
class Bar | |
def a | |
@a ||= Foo.new | |
end | |
def baz | |
a.to_proc(hello: method(:hello)).call | |
end | |
def hello(a) | |
p :hello | |
end | |
end | |
bar = Bar.new | |
a = bar.a | |
a.add { hello } | |
a.add { hola } | |
a.add { p 2 } | |
a.add { |a| p a } | |
bar.baz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment