Module: Import
- Defined in:
- lib/import.rb
Constant Summary collapse
- VERSION =
self.import('./import/version')::VERSION
Class Method Summary collapse
- .evaluate(path) ⇒ Module
-
.exist_file(path, fname = nil) ⇒ String|nil
If file exists, returns full path.
- .find_file(feature, base) ⇒ String|nil
- .import(feature, base = caller_locations[0].absolute_path) ⇒ Object
Class Method Details
.evaluate(path) ⇒ Module
48 49 50 51 52 53 54 55 |
# File 'lib/import.rb', line 48 def evaluate(path) script = File.read(path) res = Module.new res.module_eval(script, path) return res end |
.exist_file(path, fname = nil) ⇒ String|nil
If file exists, returns full path. unless, returns nil
9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/import.rb', line 9 def exist_file(path, fname = nil) pn = Pathname.new(path) pn = pn.join(fname) if fname full_path = pn.to_s return full_path if FileTest.file?(full_path) rb ="#{full_path}.rb" return rb if FileTest.file?(rb) return nil end |
.find_file(feature, base) ⇒ String|nil
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/import.rb', line 25 def find_file(feature, base) # absolute path if feature[0] == '/' full_path = exist_file(feature) return full_path if full_path end # relative path if feature.start_with?('./') || feature.start_with?('../') full_path = exist_file(File.("../#{feature}", base)) return full_path if full_path end $LOAD_PATH.each do |path| full_path = exist_file(path, feature) return full_path if full_path end return nil end |
.import(feature, base = caller_locations[0].absolute_path) ⇒ Object
59 60 61 62 63 64 |
# File 'lib/import.rb', line 59 def import(feature, base = caller_locations[0].absolute_path) path = find_file(feature, base) raise LoadError, "cannot load such file -- #{feature}" unless path return evaluate(path) end |