Class: Object

Inherits:
BasicObject
Defined in:
lib/adsl/extract/meta.rb

Instance Method Summary collapse

Instance Method Details

#replace_method(method_name, source = nil, &block) ⇒ Object



5
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
# File 'lib/adsl/extract/meta.rb', line 5

def replace_method(method_name, source = nil, &block)
  raise "Object #{self} of class #{self.class} does not respond to #{method_name}" unless self.respond_to? method_name, true
  
  im = self.singleton_class.instance_method(method_name)
  
  aliases = []
  self.singleton_class.instance_methods.each do |other_name|
    next if other_name == method_name
    other = self.singleton_class.instance_method other_name
    aliases << [other_name, other] if other == im
  end

  owner = im.owner

  unless source.nil?
    owner.class_eval source
  else
    owner.send :define_method, method_name, &block
  end

  aliases.each do |other_name, other|
    other.owner.class_exec do
      alias_method other_name, method_name
    end
  end

  true
end