Module: ApplicationModule::Autoloading

Defined in:
lib/application_module/autoloading.rb

Instance Method Summary collapse

Instance Method Details

#autoload_file(filepath) ⇒ Object



3
4
5
6
# File 'lib/application_module/autoloading.rb', line 3

def autoload_file(filepath)
  klass_name = filepath[%r{([^/]+)\.rb$}, 1].camelize
  autoload klass_name.to_sym, filepath
end

#autoload_without_namespacing(dirnames) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/application_module/autoloading.rb', line 8

def autoload_without_namespacing dirnames
  @autoload_without_namespacing_dirs ||= []
  Array(dirnames).each do |dirname|
    @autoload_without_namespacing_dirs << dirname
    Dir[path.join(dirname, "*_#{ActiveSupport::Inflector.singularize(dirname)}.rb")].each do |p|
      autoload_file(p)
    end
    Dir[path.join(dirname, "*.rb")].each do |p|
      autoload_file(p)
    end
  end
end

#const_missing(const_name) ⇒ Object

Overrides const_missing in ActiveSupport::Dependencies



22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/application_module/autoloading.rb', line 22

def const_missing(const_name)
  filename = const_name.to_s.underscore
  dirs = [nil] + (@autoload_without_namespacing_dirs || [])
  dirs.each do |dir|
    filepath = path.join(*([dir, "#{filename}.rb"].compact))
    if File.exists? filepath
      load filepath.to_s
      return const_get(const_name)
    end
  end
  super
end