mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Cache Module#parent_name
This commit is contained in:
parent
ae9356ae9e
commit
269c6c6bcf
1 changed files with 17 additions and 6 deletions
|
@ -1,4 +1,14 @@
|
||||||
class Module
|
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.
|
# Returns the module which contains this one according to its name.
|
||||||
#
|
#
|
||||||
# module M
|
# module M
|
||||||
|
@ -16,8 +26,7 @@ class Module
|
||||||
# p Module.new.parent # => Object
|
# p Module.new.parent # => Object
|
||||||
#
|
#
|
||||||
def parent
|
def parent
|
||||||
parent_name = name.split('::')[0..-2] * '::'
|
parent_name ? parent_name.constantize : Object
|
||||||
parent_name.empty? ? Object : parent_name.constantize
|
|
||||||
end
|
end
|
||||||
|
|
||||||
# Returns all the parents of this module according to its name, ordered from
|
# Returns all the parents of this module according to its name, ordered from
|
||||||
|
@ -35,11 +44,13 @@ class Module
|
||||||
#
|
#
|
||||||
def parents
|
def parents
|
||||||
parents = []
|
parents = []
|
||||||
parts = name.split('::')[0..-2]
|
if parent_name
|
||||||
|
parts = parent_name.split('::')
|
||||||
until parts.empty?
|
until parts.empty?
|
||||||
parents << (parts * '::').constantize
|
parents << (parts * '::').constantize
|
||||||
parts.pop
|
parts.pop
|
||||||
end
|
end
|
||||||
|
end
|
||||||
parents << Object unless parents.include? Object
|
parents << Object unless parents.include? Object
|
||||||
parents
|
parents
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue