Module: RequireRb
- Defined in:
- lib/requirerb.rb
Class Method Summary collapse
- .external_import(name) ⇒ Object
- .internal_import(dirname, basename) ⇒ Object
- .main(cmd, args, opts) ⇒ Object
- .run(args, opts) ⇒ Object
Class Method Details
.external_import(name) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/requirerb.rb', line 32 def self.external_import(name) if @cache.include? name puts "Already cached external module #{name}" return @cache[name] end sym = name.to_sym if Module.constants.include? sym puts "Already loaded external module #{name}" resolved = eval name @cache[name] = resolved return resolved end puts "Loading external module #{name}" snapshot = Module.constants require name defined = Module.constants - snapshot plucked = [] resolved = {} while defined.length > 0 todo = defined.pop plucked.push todo str = todo.to_s const = eval str next if const.nil? resolved[str] = const.class == Module ? Class.new.extend(const) : const if const.respond_to? :constants defined += const.constants.map {|child| "#{str}::#{child.to_s}"} end end puts "Found #{resolved}" @cache[name] = resolved plucked .select {|item| item.class == Symbol} .each {|item| Object.send(:remove_const, item)} return resolved end |
.internal_import(dirname, basename) ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/requirerb.rb', line 14 def self.internal_import(dirname, basename) path = File.join @basepath, dirname, basename if @cache.include? @path puts "Already cached local module #{path}" return @cache[path] end puts "Loading local module #{path}" prev_dirname = @dirname prev_path = @path @dirname = dirname @path = path require @path @dirname = prev_dirname @path = prev_path return @cache[path] end |
.main(cmd, args, opts) ⇒ Object
83 84 85 86 87 88 89 90 |
# File 'lib/requirerb.rb', line 83 def self.main(cmd, args, opts) case cmd when 'run' run(args, opts) else raise "Invalid command #{cmd}" end end |
.run(args, opts) ⇒ Object
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/requirerb.rb', line 72 def self.run(args, opts) file = args[0] abs = "#{Dir.pwd}/#{file}" dirname = File.dirname abs basename = File.basename abs puts "Set default basepath to #{dirname}" @basepath = dirname internal_import '', basename end |