Module: SkinnyControllers::Lookup::Namespace

Defined in:
lib/skinny_controllers/lookup/namespace.rb

Class Method Summary collapse

Class Method Details

.create_namespace(desired_namespace) ⇒ Object

iterate through the namespaces until one doesn’t exist, and create the rest



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/skinny_controllers/lookup/namespace.rb', line 7

def create_namespace(desired_namespace)
  if klass = desired_namespace.safe_constantize
    return klass
  end

  namespaces = desired_namespace.split('::')
  existing = []
  previous = Object

  namespaces.each do |namespace|
    current = (existing + [namespace]).join('::')
    begin
      Object.const_get(current)
    rescue NameError => e
      previous.const_set(namespace, Module.new)
    end

    existing << namespace
    previous = current.constantize
  end

  previous
end