Module: Tool::Autoloader
- Defined in:
- lib/tool/autoloader.rb
Constant Summary collapse
- CAPITALIZE =
%w[version cli]
Class Method Summary collapse
- .append_features(base) ⇒ Object
- .caller_dir ⇒ Object
- .capitalize(*args) ⇒ Object
- .constant_for(file) ⇒ Object
- .path_for(constant, full = false) ⇒ Object
- .setup(container, path = caller_dir) ⇒ Object
Class Method Details
.append_features(base) ⇒ Object
48 49 50 |
# File 'lib/tool/autoloader.rb', line 48 def self.append_features(base) setup(base) end |
.caller_dir ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/tool/autoloader.rb', line 52 def self.caller_dir caller.each do |line| file = File.(line.split(':', 2).first) return File.dirname(file) if file != File.(__FILE__) end File.dirname($0) end |
.capitalize(*args) ⇒ Object
44 45 46 |
# File 'lib/tool/autoloader.rb', line 44 def self.capitalize(*args) CAPITALIZE.concat(args) end |
.constant_for(file) ⇒ Object
39 40 41 42 |
# File 'lib/tool/autoloader.rb', line 39 def self.constant_for(file) return file.upcase if CAPITALIZE.include? file file.split('.', 2).first.split(/[_-]/).map(&:capitalize).join end |
.path_for(constant, full = false) ⇒ Object
30 31 32 33 34 35 36 37 |
# File 'lib/tool/autoloader.rb', line 30 def self.path_for(constant, full = false) name = constant.name.dup name = name[/[^:]+$/] unless full name.gsub! /([A-Z]+)([A-Z][a-z])/,'\1_\2' name.gsub! /([a-z\d])([A-Z])/,'\1_\2' name.gsub! '::', '/' name.tr("-", "_").downcase end |
.setup(container, path = caller_dir) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/tool/autoloader.rb', line 5 def self.setup(container, path = caller_dir) prefix = path_for(container) full_prefix = path_for(container, true) path = File.(prefix, path) directories = [] Dir.glob("#{path}/*") do |file| base = File.basename(file, '.rb') const = constant_for(base) lib = "#{full_prefix}/#{base}" if File.directory? file directories << const elsif file.end_with? '.rb' container.autoload const, lib end end directories.each do |const| next if container.const_defined? const nested = container.const_set(const, Module.new) setup nested, path end end |