Module: RubyDecorators

Defined in:
lib/ruby_decorators.rb,
lib/ruby_decorators/version.rb

Defined Under Namespace

Classes: Stack

Constant Summary collapse

VERSION =
"0.0.3"

Instance Method Summary collapse

Instance Method Details

#method_added(method_name) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ruby_decorators.rb', line 11

def method_added(method_name)
  super

  @methods    ||= {}
  @decorators ||= {}

  return if RubyDecorators::Stack.all.empty?

  @methods[method_name]    = instance_method(method_name)
  @decorators[method_name] = RubyDecorators::Stack.all.pop(42)

  class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
    #{method_visibility_for(method_name)}
    def #{method_name}(*args, &blk)
      decorators = self.class.instance_variable_get(:@decorators)[:#{method_name}]
      method     = self.class.instance_variable_get(:@methods)[:#{method_name}]

      decorators.inject(method.bind(self)) do |method, decorator|
        decorator = decorator.new if decorator.respond_to?(:new)
        lambda { |*a, &b| decorator.call(method, *a, &b) }
      end.call(*args, &blk)
    end
  RUBY_EVAL
end