Skip to content
Newer
Older
100644 40 lines (35 sloc) 987 Bytes
2732713 @lsegal Initial commit
authored Jul 15, 2009
1 class Decorator
2 class << self
3 attr_reader :decorators
4 attr_accessor :current_decorator
5 def inherited(subclass) (@decorators ||= []).push(subclass.new) end
6 end
7
8 def call(*args, &block)
9 Decorator.current_decorator = [self, args, block]
10 rescue => e
11 lines = e.backtrace
12 lines.shift
13 e.set_backtrace(lines)
14 raise e, []
15 end
16 end
17
18 class MyDecorator < Decorator
19 def decorate(klass, meth, opts = {}) p opts end
20 end
21
22 class Object
23 def self.inherited(subclass)
24 Decorator.decorators.each do |decorator|
25 name = decorator.class.to_s.split("::").last.downcase
26 subclass.instance_variable_set("@#{name}", decorator)
27 end
28 end
29
30 def self.method_added(meth)
31 if obj = Decorator.current_decorator
32 obj[0].decorate(self, meth, *obj[1], &obj[2])
33 end
34 rescue ArgumentError => e
35 raise ArgumentError, "invalid `#decorate` implementation in #{obj[0].class}", e.backtrace
36 ensure
37 Decorator.current_decorator = nil
38 end
39 end
Something went wrong with that request. Please try again.