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.4.1 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.7.1 lib/active_support/core_ext/module/introspection.rb
rubypitaya-3.12.2 ./lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/activesupport-7.0.4/lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.4 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.7 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.3.1 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.6.1 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.6 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.3 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.2.4 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.5.1 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.5 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.2.3 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.4.7 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.2.2 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.4.6 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.4.5 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.2.1 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.2 lib/active_support/core_ext/module/introspection.rb
ric-0.14.2 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4.4/lib/active_support/core_ext/module/introspection.rb