Module: RubyDecorators

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

Defined Under Namespace

Classes: Stack

Constant Summary collapse

VERSION =
"0.0.2"

Instance Method Summary collapse

Instance Method Details

#method_added(method_name) ⇒ Object



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

def method_added(method_name)
  @__decorated_methods ||= []

  return if RubyDecorators::Stack.decorators.empty?  ||
            method_name.to_s =~ /__undecorated_/     ||
            @__decorated_methods.include?(method_name)

  current_decorator = RubyDecorators::Stack.decorators.pop
  method_visibility = detect_method_visibility(method_name)

  class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
    alias_method :__undecorated_#{method_name}, :#{method_name}

    @__decorators ||= {}
    @__decorators["#{method_name}"] = current_decorator

    #{method_visibility}
    def #{method_name}(*args, &blk)
      decorator = #{current_decorator}.new
      decorator ||= self.class.instance_variable_get(:@__decorators)["#{method_name}"]

      if args.any?
        decorator.call(method(:__undecorated_#{method_name}), *args, &blk)
      else
        decorator.call(method(:__undecorated_#{method_name}), &blk)
      end
    end
  RUBY_EVAL

  @__decorated_methods << method_name
end