Class: Module

Inherits:
Object
  • Object
show all
Defined in:
lib/module_shims/module_ext.rb

Instance Method Summary collapse

Instance Method Details

#copy_existing_instance_methods(mod, template = mod) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/module_shims/module_ext.rb', line 28

def copy_existing_instance_methods(mod, template = mod)
  ['public', 'private', 'protected'].each do |perm|
    perm_s = "#{perm}_"
    template.send("#{perm_s}instance_methods", false).each do |m|
      define_method(m, mod.instance_method(m))
      send("#{perm}", m)
    end
  end
end

#instance_method_inheritance_list(instance_method, location = true) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
# File 'lib/module_shims/module_ext.rb', line 2

def instance_method_inheritance_list(instance_method, location = true)
  ancestors.find_all do |ancestor|
    (ancestor.public_instance_methods(false) +
      ancestor.protected_instance_methods(false) +
      ancestor.private_instance_methods(false)).include?(instance_method)
  end
  .map do |x|
    m = x.instance_method(instance_method)
    m.respond_to?(:super_method) && m.super_method ? m.super_method : m
  end
  .map { |m| location ? m.source_location : m }.uniq
end

#prefix_existing_instance_methods(prefix, template = self, remove_prefix = false) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/module_shims/module_ext.rb', line 38

def prefix_existing_instance_methods(prefix, template = self, remove_prefix = false)
  ['public', 'private', 'protected'].each do |perm|
    perm_s = "#{perm}_"

    template.send("#{perm_s}instance_methods", false).each do |m|
      new_name, old_name =
        if remove_prefix
          template == self ? [m[prefix.size..-1], m] : [m, "#{prefix}#{m}"]
        else
          ["#{prefix}#{m}", m]
        end

      next unless send("#{perm_s}instance_methods", false).include?(old_name.to_sym)

      define_method(new_name, instance_method(old_name))
      send("#{perm}", new_name)
      remove_method(old_name)
    end
  end
end

#remove_existing_instance_methods(mod) ⇒ Object

def supermodule(target = self)

ancestors[ancestors.index(target) + 1]

end



19
20
21
22
23
24
25
26
# File 'lib/module_shims/module_ext.rb', line 19

def remove_existing_instance_methods(mod)
  methods = Module === mod ?
    (mod.public_instance_methods(false) + mod.private_instance_methods(false) + mod.protected_instance_methods(false)) : Array(mod)

  methods.each do |m|
    remove_method(m) rescue nil
  end
end

#remove_instance_methods_from_ancestors(anc_name) ⇒ Object



59
60
61
62
63
64
# File 'lib/module_shims/module_ext.rb', line 59

def remove_instance_methods_from_ancestors(anc_name)
  ancestors.each do |anc|
    # just reset all SHIM module
    anc.remove_existing_instance_methods(anc) if anc.name == anc_name
  end
end