Module: ModuleExtensions::AutoloadTree

Included in:
Module
Defined in:
lib/module_extensions/autoload_tree.rb,
lib/module_extensions/autoload_tree.rb

Overview

class Module

Instance Method Summary collapse

Instance Method Details

#autoload_tree(dir, recursive = true, &block) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/module_extensions/autoload_tree.rb', line 9

def autoload_tree(dir, recursive=true, &block)
  pdir = Pathname.new(dir)
  pdir.each_entry do |p|
    next if p.to_s =~ /^\./
    pfull = pdir + p
    if pfull.directory? and recursive
      name = p.to_s.capitalize!
      const_set(name, Module.new) unless const_defined?(name)
      const_get(name).autoload_tree(pfull, recursive, &block)
    elsif pfull.file? and p.to_s =~ /\.rb$/
      autoload(name.to_sym, pfull.to_s) if name = block[p]
    end
  end
end

#autoloaded_module(file, recursive = true) ⇒ Object



24
25
26
27
28
29
# File 'lib/module_extensions/autoload_tree.rb', line 24

def autoloaded_module ( file, recursive=true )
  dir = file.sub(/\.rb$/, '').to_path
  autoload_tree(dir) do |path|
    path.basename.to_s.sub(/\.rb$/, '').gsub(/(?:^|_)([a-z])/) { $1.upcase }
  end
end