Module: CLIForge::AutoloadConvention

Included in:
CLIForge, EmbeddedCommands
Defined in:
lib/cli_forge/autoload_convention.rb

Instance Method Summary collapse

Instance Method Details

#const_missing(sym) ⇒ Object

‘autoload` is dead, and we don’t want to deal with its removal in 2.0, so here’s a thread-unsafe poor man’s solution.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/cli_forge/autoload_convention.rb', line 15

def const_missing(sym)
  full_sym   = "#{self.name}::#{sym}"
  path_parts = full_sym.split("::").map { |part|
    part.gsub! /([^A-Z])([A-Z]+)/,       "\\1_\\2" # OneTwo -> One_Two
    part.gsub! /([A-Z]+)([A-Z][^A-Z]+)/, "\\1_\\2" # ABCOne -> ABC_One

    part.downcase
  }

  require File.join(*path_parts)

  # Make sure that we don't get stuck in an endless loop.  `const_get` calls
  # into `const_missing`, so we can't use that.
  eval "defined?(#{full_sym}) ? #{full_sym} : super"
end