mirror of
https://github.com/github/rails.git
synced 2026-01-27 23:38:11 -05:00
Cache Module#parent_name
This commit is contained in:
@@ -1,4 +1,14 @@
|
||||
class Module
|
||||
# Returns the name of the module containing this one.
|
||||
#
|
||||
# p M::N.parent_name # => "M"
|
||||
def parent_name
|
||||
unless defined? @parent_name
|
||||
@parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
|
||||
end
|
||||
@parent_name
|
||||
end
|
||||
|
||||
# Returns the module which contains this one according to its name.
|
||||
#
|
||||
# module M
|
||||
@@ -16,8 +26,7 @@ class Module
|
||||
# p Module.new.parent # => Object
|
||||
#
|
||||
def parent
|
||||
parent_name = name.split('::')[0..-2] * '::'
|
||||
parent_name.empty? ? Object : parent_name.constantize
|
||||
parent_name ? parent_name.constantize : Object
|
||||
end
|
||||
|
||||
# Returns all the parents of this module according to its name, ordered from
|
||||
@@ -35,10 +44,12 @@ class Module
|
||||
#
|
||||
def parents
|
||||
parents = []
|
||||
parts = name.split('::')[0..-2]
|
||||
until parts.empty?
|
||||
parents << (parts * '::').constantize
|
||||
parts.pop
|
||||
if parent_name
|
||||
parts = parent_name.split('::')
|
||||
until parts.empty?
|
||||
parents << (parts * '::').constantize
|
||||
parts.pop
|
||||
end
|
||||
end
|
||||
parents << Object unless parents.include? Object
|
||||
parents
|
||||
|
||||
Reference in New Issue
Block a user