Mention AroundDecorator in README.txt.
[decorate.git] / lib / decorate / create_alias.rb
blobe0e3a138fb3b8e413f1a4f57d45b974bad8bec96
1 module Decorate
3   # Create a private alias for the instance method named +method_name+
4   # of class +klass+. The string representation of +id+ will be part
5   # of the alias to ease debugging. This method makes sure that the
6   # alias doesn't redefine an existing method.
7   #
8   # Returns the name of the new alias.
9   #
10   # In the simplest case, the alias will be
11   # <tt>"#{method_name}_without_#{id}"</tt>.
12   def self.create_alias(klass, method_name, id)
13     basename = "#{method_name}_without_#{id}"
15     i = 0
16     new_name = basename
17     loop {
18       break unless klass.method_defined?(new_name)
19       i += 1
20       new_name = "#{basename}_#{i}"
21     }
23     klass.send(:alias_method, new_name, method_name)
24     klass.send(:private, new_name)
25     new_name
26   end
28 end