Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 81 lines (75 sloc) 1.965 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 #
31 def parent
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
32 parent_name ? ActiveSupport::Inflector.constantize(parent_name) : Object
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
33 end
dc604b4 @jeremy Ruby 1.9: Module#local_constants can now just use constants(false). C…
jeremy authored
34
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
35 # Returns all the parents of this module according to its name, ordered from
36 # nested outwards. The receiver is not contained within the result.
37 #
38 # module M
39 # module N
40 # end
41 # end
42 # X = M::N
43 #
81535d0 @fxn removes p calls in rdoc examples
fxn authored
44 # M.parents # => [Object]
45 # M::N.parents # => [M, Object]
46 # X.parents # => [M, Object]
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
47 #
48 def parents
49 parents = []
50 if parent_name
51 parts = parent_name.split('::')
52 until parts.empty?
f28bd95 @jeremy Fix dependencies revealed by testing in isolation
jeremy authored
53 parents << ActiveSupport::Inflector.constantize(parts * '::')
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
54 parts.pop
269c6c6 @jeremy Cache Module#parent_name
jeremy authored
55 end
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
56 end
57 parents << Object unless parents.include? Object
58 parents
59 end
dc604b4 @jeremy Ruby 1.9: Module#local_constants can now just use constants(false). C…
jeremy authored
60
7ab4775 @josevalim Initial pass at removing dead 1.8.x code from Active Support.
josevalim authored
61 def local_constants #:nodoc:
62 constants(false)
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
63 end
64
04df7bb @fxn deprecates Module#local_constant_names
fxn authored
65 # *DEPRECATED*: Use +local_constants+ instead.
66 #
67 # Returns the names of the constants defined locally as strings.
68 #
69 # module M
70 # X = 1
71 # end
72 # M.local_constant_names # => ["X"]
73 #
74 # This method is useful for forward compatibility, since Ruby 1.8 returns
75 # constant names as strings, whereas 1.9 returns them as symbols.
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
76 def local_constant_names
9257224 @gazay AS core_ext refactoring pt.2
gazay authored
77 ActiveSupport::Deprecation.warn 'Module#local_constant_names is deprecated, use Module#local_constants instead', caller
dc0c916 @jeremy Convert Module aliasing and introspection extension modules to class …
jeremy authored
78 local_constants.map { |c| c.to_s }
c64a29f @jeremy Ruby 1.9 compat: dependencies uses Module#local_constant_names. Refer…
jeremy authored
79 end
9efca53 @seckar Dependencies cleanup. Fixes #4221.
seckar authored
80 end
Something went wrong with that request. Please try again.