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
ric-0.14.1 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4.4/lib/active_support/core_ext/module/introspection.rb
phillipug-foodie-0.1.0 .vendor/ruby/3.0.0/gems/activesupport-6.1.4.4/lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.1 lib/active_support/core_ext/module/introspection.rb
ric-0.14.0 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.4.4/lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.0 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.4.4 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.0.rc3 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.4.3 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.4.2 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.0.rc2 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.0.rc1 lib/active_support/core_ext/module/introspection.rb
date_n_time_picker_activeadmin-0.1.2 vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/core_ext/module/introspection.rb
date_n_time_picker_activeadmin-0.1.1 vendor/bundle/ruby/2.6.0/gems/activesupport-6.1.4.1/lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.0.alpha2 lib/active_support/core_ext/module/introspection.rb
activesupport-7.0.0.alpha1 lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.4.1 lib/active_support/core_ext/module/introspection.rb
rails_mini_profiler-0.2.0 vendor/bundle/ruby/3.0.0/gems/activesupport-6.1.4/lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.4 lib/active_support/core_ext/module/introspection.rb
tdiary-5.1.6 vendor/bundle/ruby/2.7.0/gems/activesupport-6.1.3.1/lib/active_support/core_ext/module/introspection.rb
activesupport-6.1.3.2 lib/active_support/core_ext/module/introspection.rb