Module: Aef::NamespaceHelper::ClassMethods

Defined in:
lib/aef/namespace_helper.rb

Overview

This mixin module is intended to extend a single class/module object to make the helper methods only available on that specific class/module.

Examples:

First::Second.extend(Aef::NamespaceHelper::ClassMethods)
First::Second.respond_to?(:unprefixed_name) # => true

See Also:

Instance Method Summary collapse

Instance Method Details

#namespace_component_namesArray<String>

Note:

This will not reference the class/module objects and thus won’t trigger autoloading

Lists all namespace components’ names.

Examples:

First::Second::Third.namespace_component_names
# => ["First", "First::Second", "First::Second::Third"]

Returns:

  • (Array<String>)

    All namespace components’ names. Ordered, so the class/module itself will be the last element

Since:

  • 1.0.0



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 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

#namespace_componentsArray<Class, Module>

Lists all namespace components.

Examples:

First::Second::Third.namespace_component_names
# => [First, First::Second, First::Second::Third]

Returns:

  • (Array<Class, Module>)

    All namespace components. Ordered, so the class/module itself will be the last element.

Since:

  • 1.0.0



120
121
122
123
124
125
126
127
128
129
130
131
132
# 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

#namespace_parentClass, Module

The component in the namespace which encapsulates this class/module.

Examples:

First::Second::Third.namespace_parent
# => First::Second

Returns:

  • (Class, Module)

    The parent namespace component

Since:

  • 1.0.0



153
154
155
# File 'lib/aef/namespace_helper.rb', line 153

def namespace_parent
  namespace_components[-2]
end

#namespace_parent_nameString

Note:

This will not reference the class/module objects and thus won’t trigger autoloading

The name of component in the namespace which encapsulates this class/module.

Examples:

First::Second::Third.namespace_parent
# => First::Second

Returns:

  • (String)

    The name of the parent namespace component

Since:

  • 1.0.0



142
143
144
# File 'lib/aef/namespace_helper.rb', line 142

def namespace_parent_name
  namespace_component_names[-2]
end

#unprefixed_nameString

The name of the class/module itself, without any namespacing.

Examples:

First::Second::Third.unprefixed_name # => "Third"

Returns:

  • (String)

    The unprefixed name of the class/module itself

Since:

  • 1.0.0



63
64
65
# File 'lib/aef/namespace_helper.rb', line 63

def unprefixed_name
  unprefixed_namespace_component_names.last
end

#unprefixed_namespace_component_namesArray<String>

Note:

This will not reference the class/module objects and thus won’t trigger autoloading

Lists all namespace components’ unprefixed names.

Examples:

First::Second::Third.unprefixed_namespace_component_names
# => ["First", "Second", "Third"]

Returns:

  • (Array<String>)

    All namespace components’ unprefixed names. Ordered, so the class/module itself will be the last element

Since:

  • 1.0.0



77
78
79
80
81
82
83
# 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