Sha256: c9811f18a0cd3ebda9a22ffcf11b69db3caa3f5f62ba7d75397485599be9e5cc

Contents?: true

Size: 1.38 KB

Versions: 23

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
      @parent_name = parent_name unless frozen?
      parent_name
    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
end

Version data entries

23 entries across 23 versions & 3 rubygems

Version Path
activesupport-5.1.7 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.7.rc1 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.6.2 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.6.1 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.6 lib/active_support/core_ext/module/introspection.rb
tdiary-5.0.8 vendor/bundle/gems/activesupport-5.1.5/lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.5 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.5.rc1 lib/active_support/core_ext/module/introspection.rb
pract6-0.1.0 .gem/ruby/2.3.0/gems/activesupport-5.1.4/lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.4 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.4.rc1 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.3 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.3.rc3 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.3.rc2 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.3.rc1 lib/active_support/core_ext/module/introspection.rb
tdiary-5.0.5 vendor/bundle/gems/activesupport-5.1.2/lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.2 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.2.rc1 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.1 lib/active_support/core_ext/module/introspection.rb
activesupport-5.1.0 lib/active_support/core_ext/module/introspection.rb