Sha256: c3e9dab0e008a4d3c579b25928e87af0351c595e7762b8f7abf2c0bbf650ad3f

Contents?: true

Size: 1.38 KB

Versions: 106

Compression:

Stored size: 1.38 KB

Contents

require 'active_support/inflector'

class Module
  # Returns the name of the module containing this one.
  #
  #   M::N.parent_name # => "M"
  def parent_name
    if defined? @parent_name
      @parent_name
    else
      @parent_name = name =~ /::[^:]+\Z/ ? $`.freeze : nil
    end
  end

  # Returns the module which contains this one according to its name.
  #
  #   module M
  #     module N
  #     end
  #   end
  #   X = M::N
  #
  #   M::N.parent # => M
  #   X.parent    # => M
  #
  # The parent of top-level and anonymous modules is Object.
  #
  #   M.parent          # => Object
  #   Module.new.parent # => Object
  def parent
    parent_name ? ActiveSupport::Inflector.constantize(parent_name) : Object
  end

  # Returns all the parents of this module according to its name, ordered from
  # nested outwards. The receiver is not contained within the result.
  #
  #   module M
  #     module N
  #     end
  #   end
  #   X = M::N
  #
  #   M.parents    # => [Object]
  #   M::N.parents # => [M, Object]
  #   X.parents    # => [M, Object]
  def parents
    parents = []
    if parent_name
      parts = parent_name.split('::')
      until parts.empty?
        parents << ActiveSupport::Inflector.constantize(parts * '::')
        parts.pop
      end
    end
    parents << Object unless parents.include? Object
    parents
  end

  def local_constants #:nodoc:
    constants(false)
  end
end

Version data entries

106 entries across 101 versions & 12 rubygems

Version Path
activesupport-4.2.11.3 lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.11.2 lib/active_support/core_ext/module/introspection.rb
cocoapods-dependency-html-0.0.2 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/module/introspection.rb
cocoapods-dependency-html-0.0.1 vendor/bundle/gems/activesupport-4.2.11.1/lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.11.1 lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.11 lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.10 lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.10.rc1 lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.9 lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.9.rc2 lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.9.rc1 lib/active_support/core_ext/module/introspection.rb
enju_leaf-1.2.1 vendor/bundle/ruby/2.3/gems/activesupport-4.2.8/lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.8 lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.8.rc1 lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.7.1 lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.7 lib/active_support/core_ext/module/introspection.rb
activesupport-4.1.16 lib/active_support/core_ext/module/introspection.rb
activesupport-4.1.16.rc1 lib/active_support/core_ext/module/introspection.rb
activesupport-4.2.7.rc1 lib/active_support/core_ext/module/introspection.rb
ish_lib_manager-0.0.1 test/dummy/vendor/bundle/ruby/2.3.0/gems/activesupport-4.2.6/lib/active_support/core_ext/module/introspection.rb