module Aef::NamespaceHelper::ClassMethods
This mixin module is intended to extend a single class/module object to make the helper methods only available on that specific class/module.
@example
First::Second.extend(Aef::NamespaceHelper::ClassMethods) First::Second.respond_to?(:unprefixed_name) # => true
@see Aef::NamespaceHelper
Public Instance Methods
Lists all namespace components’ names.
@return [Array<String>] All namespace components’ names. Ordered, so
the class/module itself will be the last element
@since 1.0.0 @note This will not reference the class/module objects and thus won’t
trigger autoloading
@example
First::Second::Third.namespace_component_names # => ["First", "First::Second", "First::Second::Third"]
# File lib/aef/namespace_helper.rb, line 95 def namespace_component_names if self.name.nil? || self.name == '' [] else unprefixed_names = unprefixed_namespace_component_names names = [] while current_name = unprefixed_names.pop current_full_name = (unprefixed_names + [current_name]).join('::') names.unshift(current_full_name) end names end end
Lists all namespace components.
@return [Array<Class, Module>] All namespace components. Ordered, so
the class/module itself will be the last element.
@since 1.0.0 @example
First::Second::Third.namespace_component_names # => [First, First::Second, First::Second::Third]
# File lib/aef/namespace_helper.rb, line 120 def namespace_components components = [] unprefixed_names = unprefixed_namespace_component_names current_component = Object while current_name = unprefixed_names.shift current_component = current_component.const_get(current_name) components << current_component end components end
The component in the namespace which encapsulates this class/module.
@return [Class, Module] The parent namespace component @since 1.0.0 @example
First::Second::Third.namespace_parent # => First::Second
# File lib/aef/namespace_helper.rb, line 153 def namespace_parent namespace_components[-2] end
The name of component in the namespace which encapsulates this class/module.
@return [String] The name of the parent namespace component @since 1.0.0 @note This will not reference the class/module objects and thus won’t trigger autoloading @example
First::Second::Third.namespace_parent # => First::Second
# File lib/aef/namespace_helper.rb, line 142 def namespace_parent_name namespace_component_names[-2] end
The name of the class/module itself, without any namespacing.
@return [String] The unprefixed name of the class/module itself @since 1.0.0 @example
First::Second::Third.unprefixed_name # => "Third"
# File lib/aef/namespace_helper.rb, line 63 def unprefixed_name unprefixed_namespace_component_names.last end
Lists all namespace components’ unprefixed names.
@return [Array<String>] All namespace components’ unprefixed names.
Ordered, so the class/module itself will be the last element
@since 1.0.0 @note This will not reference the class/module objects and thus won’t
trigger autoloading
@example
First::Second::Third.unprefixed_namespace_component_names # => ["First", "Second", "Third"]
# File lib/aef/namespace_helper.rb, line 77 def unprefixed_namespace_component_names if self.name.nil? || self.name == '' [] else self.name.split('::') end end