Sha256: ff1427cdd4edcbc5d1980f890a92d7378757e47ebd340b175844f0b432d4229e
Contents?: true
Size: 1.72 KB
Versions: 1
Compression:
Stored size: 1.72 KB
Contents
require "method_decorators/version" module MethodDecorators def method_added(name) super orig_method = instance_method(name) decorators = MethodDecorator.current_decorators return if decorators.empty? if private_method_defined?(name); visibility = :private elsif protected_method_defined?(name); visibility = :protected else visibility = :public end define_method(name) do |*args, &blk| decorated = MethodDecorators.decorate_callable(orig_method.bind(self), decorators) decorated.call(*args, &blk) end case visibility when :protected; protected name when :private; private name end end def singleton_method_added(name) super orig_method = method(name) decorators = MethodDecorator.current_decorators return if decorators.empty? MethodDecorators.define_others_singleton_method(self, name) do |*args, &blk| decorated = MethodDecorators.decorate_callable(orig_method, decorators) decorated.call(*args, &blk) end end def self.decorate_callable(orig, decorators) decorators.reduce(orig) do |callable, decorator| lambda{ |*a, &b| decorator.call(callable, *a, &b) } end end def self.define_others_singleton_method(klass, name, &blk) if klass.respond_to?(:define_singleton_method) klass.define_singleton_method(name, &blk) else class << klass self end.send(:define_method, name, &blk) end end end class MethodDecorator @@current_decorators = [] def self.current_decorators decs = @@current_decorators @@current_decorators = [] decs end def self.+@ +new end def +@ @@current_decorators.unshift(self) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
method_decorators-0.9.1 | lib/method_decorators.rb |