Skip to content
This repository
Newer
Older
100644 50 lines (45 sloc) 1.555 kb
9efca539 » seckar
2006-03-27 Dependencies cleanup. Fixes #4221.
1 class Module
2 # Return the module which contains this one; if this is a root module, such as
3 # +::MyModule+, then Object is returned.
4 def parent
5 parent_name = name.split('::')[0..-2] * '::'
6 parent_name.empty? ? Object : parent_name.constantize
7 end
dc604b43 » jeremy
2008-01-04 Ruby 1.9: Module#local_constants can now just use constants(false). C…
8
9efca539 » seckar
2006-03-27 Dependencies cleanup. Fixes #4221.
9 # Return all the parents of this module, ordered from nested outwards. The
10 # receiver is not contained within the result.
11 def parents
12 parents = []
13 parts = name.split('::')[0..-2]
14 until parts.empty?
15 parents << (parts * '::').constantize
16 parts.pop
17 end
18 parents << Object unless parents.include? Object
19 parents
20 end
dc604b43 » jeremy
2008-01-04 Ruby 1.9: Module#local_constants can now just use constants(false). C…
21
22 if RUBY_VERSION < '1.9'
23 # Return the constants that have been defined locally by this object and
24 # not in an ancestor. This method is exact if running under Ruby 1.9. In
25 # previous versions it may miss some constants if their definition in some
26 # ancestor is identical to their definition in the receiver.
27 def local_constants
28 inherited = {}
29
30 ancestors.each do |anc|
31 next if anc == self
32 anc.constants.each { |const| inherited[const] = anc.const_get(const) }
33 end
34
35 constants.select do |const|
36 !inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
37 end
4eec5f4f » seckar
2007-03-14 Update Dependencies to ignore constants inherited from ancestors. Clo…
38 end
dc604b43 » jeremy
2008-01-04 Ruby 1.9: Module#local_constants can now just use constants(false). C…
39 else
40 def local_constants #:nodoc:
41 constants(false)
4eec5f4f » seckar
2007-03-14 Update Dependencies to ignore constants inherited from ancestors. Clo…
42 end
43 end
c64a29f6 » jeremy
2007-12-22 Ruby 1.9 compat: dependencies uses Module#local_constant_names. Refer…
44
45 # Returns the names of the constants defined locally rather than the
46 # constants themselves. See <tt>local_constants</tt>.
47 def local_constant_names
48 local_constants.map(&:to_s)
49 end
9efca539 » seckar
2006-03-27 Dependencies cleanup. Fixes #4221.
50 end
Something went wrong with that request. Please try again.