Sha256: 333ccf3aeb0d325e1781226ac6dddfb0950f6280ccf1a210ed374cdbdead59a4

Contents?: true

Size: 1.56 KB

Versions: 88

Compression:

Stored size: 1.56 KB

Contents

# frozen_string_literal: true

require "active_support/core_ext/string/filters"
require "active_support/inflector"

class Module
  # Returns the name of the module containing this one.
  #
  #   M::N.module_parent_name # => "M"
  def module_parent_name
    if defined?(@parent_name)
      @parent_name
    else
      parent_name = name =~ /::[^:]+\z/ ? -$` : 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.module_parent # => M
  #   X.module_parent    # => M
  #
  # The parent of top-level and anonymous modules is Object.
  #
  #   M.module_parent          # => Object
  #   Module.new.module_parent # => Object
  def module_parent
    module_parent_name ? ActiveSupport::Inflector.constantize(module_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.module_parents    # => [Object]
  #   M::N.module_parents # => [M, Object]
  #   X.module_parents    # => [M, Object]
  def module_parents
    parents = []
    if module_parent_name
      parts = module_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

88 entries across 83 versions & 13 rubygems

Version Path
activesupport-7.0.8.6 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.7.10 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.7.9 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.8.5 lib/active_support/core_ext/module/introspection.rb
blacklight-spotlight-3.6.0.beta8 vendor/bundle/ruby/3.2.0/gems/activesupport-7.0.8.4/lib/active_support/core_ext/module/introspection.rb
cm-admin-1.5.22 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/module/introspection.rb
cm-admin-1.5.21 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/module/introspection.rb
cm-admin-1.5.20 vendor/bundle/ruby/3.3.0/gems/activesupport-7.0.5.1/lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.8.4 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.7.8 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.8.1 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.7.7 lib/active_support/core_ext/module/introspection.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.3.1/lib/active_support/core_ext/module/introspection.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-6.1.6.1/lib/active_support/core_ext/module/introspection.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activesupport-7.0.2.3/lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.8 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.7.2 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.7.6 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.7.1 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.7.5 lib/active_support/core_ext/module/introspection.rb