File: active_support/core_ext/module/introspection.rb

Overview
Module Structure
Code

Overview

Module Structure

  module: <Toplevel Module>
  module: ActiveSupport#1
  module: CoreExtensions#2
  module: Module#3
has properties
method: parent_name #7
method: parent #30
method: parents #47
method: local_constants (1/2) #65
method: local_constants (2/E) #78
method: local_constant_names #85

Code

   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
  13 
  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
  33 
  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
  58        end
  59 
  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
  72 
  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
  81        end
  82 
  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 }
  87        end
  88      end
  89    end
  90  end