Class: ConvenientService::Utils::Module::GetNamespace Private

Inherits:
Support::Command show all
Defined in:
lib/convenient_service/utils/module/get_namespace.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Returns parent namespace of class or module.

Examples:

Common usage.

module Foo
  module Bar
    class Baz
    end
  end
end

ConvenientService::Utils::Module::GetNamespace.call(Foo)
# => nil

ConvenientService::Utils::Module::GetNamespace.call(Foo::Bar)
# => Foo

ConvenientService::Utils::Module::GetNamespace.call(Foo::Bar::Baz)
# => Foo::Bar

ConvenientService::Utils::Module::GetNamespace.call(Module.new)
# => nil

Since:

  • 1.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Support::Command

[], call

Constructor Details

#initialize(mod) ⇒ void

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Parameters:

Since:

  • 1.0.0



48
49
50
# File 'lib/convenient_service/utils/module/get_namespace.rb', line 48

def initialize(mod)
  @mod = mod
end

Instance Attribute Details

#modObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 1.0.0



42
43
44
# File 'lib/convenient_service/utils/module/get_namespace.rb', line 42

def mod
  @mod
end

Instance Method Details

#callModule, ...

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/convenient_service/utils/module/get_namespace.rb', line 56

def call
  return unless mod.name
  return unless mod.name.include?("::")

  namespace_name = mod.name.split("::")[0..-2].join("::")

  begin
    ::Object.const_get(namespace_name, false)
  rescue ::NameError
    ::ConvenientService.raise Exceptions::NestingUnderAnonymousNamespace.new(mod: mod, namespace: namespace_name)
  end
end