sam / extlib

General Ruby extensions for DataMapper and DataObjects

This URL has Read+Write access

extlib / lib / extlib / module.rb
100644 38 lines (30 sloc) 0.967 kb
1
2
3
4
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
33
34
35
36
37
38
class Module
  def find_const(const_name)
    if const_name[0..1] == '::'
      Object.find_const(const_name[2..-1])
    else
      nested_const_lookup(const_name)
    end
  end
 
  private
 
  # Doesn't do any caching since constants can change with remove_const
  def nested_const_lookup(const_name)
    constants = [ Object ]
 
    unless self == Object
      self.name.split('::').each do |part|
        constants.unshift(constants.first.const_get(part))
      end
    end
 
    parts = const_name.split('::')
 
    # from most to least specific constant, use each as a base and try
    # to find a constant with the name const_name within them
    constants.each do |const|
      # return the nested constant if available
      return const if parts.all? do |part|
        const = const.const_defined?(part) ? const.const_get(part) : nil
      end
    end
 
    # if we get this far then the nested constant was not found
    raise NameError
  end
 
end # class Module