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
|
# File 'lib/fiveruns/tuneup/superlative.rb', line 10
def self.on(target, level = :singleton, &block)
wedge = Module.new(&block)
case level
when :singleton
save = Module.new
wedge.instance_methods.each do |meth|
save.send(:define_method, meth, target.method(meth))
if target.metaclass.instance_methods.include?(meth.to_s)
target.metaclass.send(:remove_method, meth)
end
end
target.extend save
target.extend wedge
when :instances wedges << wedge
offset = wedges.size - 1
hook = Module.new
hook.module_eval %{
def new(*args, &block)
super.extend(Fiveruns::Tuneup::Superlative.wedges[#{offset}])
end
}
target.extend hook
end
end
|