Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 91 lines (84 sloc) 2.597 kb
204a8cc Hongli Lai Move some core extension methods into a module under the ActiveSupport::...
FooBarWidget authored
1 module ActiveSupport
2 module CoreExtensions
3 module Module
4 # Returns the name of the module containing this one.
5 #
6 # p M::N.parent_name # => "M"
7 def parent_name
8 unless defined? @parent_name
9 @parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
10 end
11 @parent_name
12 end
269c6c6 Jeremy Kemper Cache Module#parent_name
jeremy authored
13
204a8cc Hongli Lai Move some core extension methods into a module under the ActiveSupport::...
FooBarWidget authored
14 # Returns the module which contains this one according to its name.
15 #
16 # module M
17 # module N
18 # end
19 # end
20 # X = M::N
21 #
22 # p M::N.parent # => M
23 # p X.parent # => M
24 #
25 # The parent of top-level and anonymous modules is Object.
26 #
27 # p M.parent # => Object
28 # p Module.new.parent # => Object
29 #
30 def parent
31 parent_name ? parent_name.constantize : Object
32 end
dc604b4 Jeremy Kemper Ruby 1.9: Module#local_constants can now just use constants(false). Clos...
jeremy authored
33
204a8cc Hongli Lai Move some core extension methods into a module under the ActiveSupport::...
FooBarWidget authored
34 # Returns all the parents of this module according to its name, ordered from
35 # nested outwards. The receiver is not contained within the result.
36 #
37 # module M
38 # module N
39 # end
40 # end
41 # X = M::N
42 #
43 # p M.parents # => [Object]
44 # p M::N.parents # => [M, Object]
45 # p X.parents # => [M, Object]
46 #
47 def parents
48 parents = []
49 if parent_name
50 parts = parent_name.split('::')
51 until parts.empty?
52 parents << (parts * '::').constantize
53 parts.pop
54 end
55 end
56 parents << Object unless parents.include? Object
57 parents
269c6c6 Jeremy Kemper Cache Module#parent_name
jeremy authored
58 end
dc604b4 Jeremy Kemper Ruby 1.9: Module#local_constants can now just use constants(false). Clos...
jeremy authored
59
204a8cc Hongli Lai Move some core extension methods into a module under the ActiveSupport::...
FooBarWidget authored
60 if RUBY_VERSION < '1.9'
61 # Returns the constants that have been defined locally by this object and
62 # not in an ancestor. This method is exact if running under Ruby 1.9. In
63 # previous versions it may miss some constants if their definition in some
64 # ancestor is identical to their definition in the receiver.
65 def local_constants
66 inherited = {}
67
68 ancestors.each do |anc|
69 next if anc == self
70 anc.constants.each { |const| inherited[const] = anc.const_get(const) }
71 end
dc604b4 Jeremy Kemper Ruby 1.9: Module#local_constants can now just use constants(false). Clos...
jeremy authored
72
204a8cc Hongli Lai Move some core extension methods into a module under the ActiveSupport::...
FooBarWidget authored
73 constants.select do |const|
74 !inherited.key?(const) || inherited[const].object_id != const_get(const).object_id
75 end
76 end
77 else
78 def local_constants #:nodoc:
79 constants(false)
80 end
dc604b4 Jeremy Kemper Ruby 1.9: Module#local_constants can now just use constants(false). Clos...
jeremy authored
81 end
82
204a8cc Hongli Lai Move some core extension methods into a module under the ActiveSupport::...
FooBarWidget authored
83 # Returns the names of the constants defined locally rather than the
84 # constants themselves. See <tt>local_constants</tt>.
85 def local_constant_names
86 local_constants.map { |c| c.to_s }
dc604b4 Jeremy Kemper Ruby 1.9: Module#local_constants can now just use constants(false). Clos...
jeremy authored
87 end
4eec5f4 Nicholas Seckar Update Dependencies to ignore constants inherited from ancestors. Closes...
seckar authored
88 end
c64a29f Jeremy Kemper Ruby 1.9 compat: dependencies uses Module#local_constant_names. Referenc...
jeremy authored
89 end
9efca53 Nicholas Seckar Dependencies cleanup. Fixes #4221.
seckar authored
90 end
Something went wrong with that request. Please try again.