RIM007 - Python-style Decorators
class BaseDecorator | |
def initialize(closure) | |
@closure = closure | |
end | |
def bind_to(receiver) | |
@closure = @closure.bind(receiver) | |
end | |
end |
class CacheDecorator < BaseDecorator | |
def call(*args) | |
puts "Before closure" | |
result = @closure.call(*args) | |
puts "After closure" | |
return result | |
end | |
end |
module FunctionDecorators | |
def self.included(receiver) | |
receiver.instance_eval { | |
def decorate(decorator) | |
@decorate_next_with = decorator | |
end | |
def method_added(name) | |
if @decorate_next_with | |
FunctionDecorators.apply_decorator(@decorate_next_with, name, self) | |
@decorate_next_with = nil | |
end | |
end | |
def __decorators | |
@_decorators ||= {} | |
end | |
} | |
end | |
def self.apply_decorator(decorator, method_name, target) | |
decorated_method = target.instance_method(method_name) | |
target.send(:remove_method, method_name) | |
target.__decorators[method_name] = decorator.new(decorated_method) | |
new_method = <<-RUBY | |
def #{method_name}(#{params = decorated_method.parameters.collect(&:last).join(',')}) | |
self.class.__decorators[:#{method_name}].bind_to(self) | |
self.class.__decorators[:#{method_name}].call(#{params}) | |
end | |
RUBY | |
class_eval new_method | |
end | |
end | |
class Object | |
include FunctionDecorators | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This comment has been minimized.
Beispiel Code zur Anwendung von Methoden als Closures aus der ersten Ausgabe von "Ruby is Magic" zum Thema Closures in Ruby: http://rubyismagic.de/blog/2012/01/19/episode-7-closures/