Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 79 lines (73 sloc) 1.949 kb
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
1 require 'active_support/inflector'
2
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
3 class Module
4 # Returns the name of the module containing this one.
5 #
81535d0 @fxn removes p calls in rdoc examples
fxn authored
6 # M::N.parent_name # => "M"
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
7 def parent_name
9257224 @gazay AS core_ext refactoring pt.2
gazay authored
8 if defined? @parent_name
9 @parent_name
10 else
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
11 @parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
12 end
13 end
269c6c6 @jeremy Cache Module#parent_name
jeremy authored
14
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
15 # Returns the module which contains this one according to its name.
16 #
17 # module M
18 # module N
19 # end
20 # end
21 # X = M::N
22 #
81535d0 @fxn removes p calls in rdoc examples
fxn authored
23 # M::N.parent # => M
24 # X.parent # => M
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
25 #
26 # The parent of top-level and anonymous modules is Object.
27 #
81535d0 @fxn removes p calls in rdoc examples
fxn authored
28 # M.parent # => Object
29 # Module.new.parent # => Object
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
30 def parent
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
31 parent_name ? ActiveSupport::Inflector.constantize(parent_name) : Object
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
32 end
dc604b4 @jeremy Ruby 1.9: Module#local_constants can now just use constants(false). C…
jeremy authored
33
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy 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 #
81535d0 @fxn removes p calls in rdoc examples
fxn authored
43 # M.parents # => [Object]
44 # M::N.parents # => [M, Object]
45 # X.parents # => [M, Object]
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
46 def parents
47 parents = []
48 if parent_name
49 parts = parent_name.split('::')
50 until parts.empty?
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
51 parents << ActiveSupport::Inflector.constantize(parts * '::')
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
52 parts.pop
269c6c6 @jeremy Cache Module#parent_name
jeremy authored
53 end
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
54 end
55 parents << Object unless parents.include? Object
56 parents
57 end
dc604b4 @jeremy Ruby 1.9: Module#local_constants can now just use constants(false). C…
jeremy authored
58
7ab4775 @josevalim Initial pass at removing dead 1.8.x code from Active Support.
josevalim authored
59 def local_constants #:nodoc:
60 constants(false)
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
61 end
62
04df7bb @fxn deprecates Module#local_constant_names
fxn authored
63 # *DEPRECATED*: Use +local_constants+ instead.
64 #
65 # Returns the names of the constants defined locally as strings.
66 #
67 # module M
68 # X = 1
69 # end
70 # M.local_constant_names # => ["X"]
71 #
72 # This method is useful for forward compatibility, since Ruby 1.8 returns
73 # constant names as strings, whereas 1.9 returns them as symbols.
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
74 def local_constant_names
b955939 @gazay Make caller attribute in deprecation methods optional
gazay authored
75 ActiveSupport::Deprecation.warn 'Module#local_constant_names is deprecated, use Module#local_constants instead'
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
76 local_constants.map { |c| c.to_s }
c64a29f @jeremy Ruby 1.9 compat: dependencies uses Module#local_constant_names. Refer…
jeremy authored
77 end
9efca53 @seckar Dependencies cleanup. Fixes #4221.
seckar authored
78 end
Something went wrong with that request. Please try again.