Module: RubyExts::ImportMixin
- Defined in:
- lib/rubyexts/mixins/import.rb
Overview
This mixin will be included also to project or your custom app main namespace
Class Method Summary collapse
-
.extended(base) ⇒ Object
class Project extend Rango::ImportMixin end.
-
.included(base) ⇒ Object
class Project class << self extend Rango::ImportMixin end end.
Instance Method Summary collapse
- #find(file) ⇒ Object
- #find_absolute(file) ⇒ Object
-
#import(path, options = Hash.new) ⇒ Boolean
If loading suceed.
-
#import!(path) ⇒ Boolean
If the loading was successful.
- #import_first(paths, options = Hash.new) ⇒ Object
Class Method Details
.extended(base) ⇒ Object
class Project
extend Rango::ImportMixin
end
11 12 13 14 15 16 17 18 19 |
# File 'lib/rubyexts/mixins/import.rb', line 11 def self.extended(base) unless base.respond_to?(:root) class << base def root File.dirname(caller[1].split(":").first) end end end end |
.included(base) ⇒ Object
class Project
class << self
extend Rango::ImportMixin
end
end
26 27 28 29 30 31 32 33 34 |
# File 'lib/rubyexts/mixins/import.rb', line 26 def self.included(base) unless base.respond_to?(:root) base.class_eval do def root File.dirname(caller.last.split(":").first) end end end end |
Instance Method Details
#find(file) ⇒ Object
78 79 80 81 82 |
# File 'lib/rubyexts/mixins/import.rb', line 78 def find(file) ["#{file}.rb", file].find do |file| File.exist?(file) end end |
#find_absolute(file) ⇒ Object
84 85 86 87 |
# File 'lib/rubyexts/mixins/import.rb', line 84 def find_absolute(file) file = File.join(self.root, file) unless file.match(%r[^/]) self.find(file) end |
#import(path, options = Hash.new) ⇒ Boolean
Returns If loading suceed.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/rubyexts/mixins/import.rb', line 44 def import(path, = Hash.new) # it is better than rescue LoadError, because # LoadError can be raise inside the required file fullpath = self.find_absolute(path) if fullpath.nil? && [:soft] # any file found and soft importing enabled # do nothing elsif fullpath.nil? && ![:soft] # any file found and soft importing disabled raise LoadError, "File #{path.inspect} (treated as #{fullpath.inspect}) doesn't exist" elsif !fullpath.nil? # the file was found Kernel.load(fullpath) elsif !fullpath.nil? # the file was found Kernel.require(fullpath) end end |
#import!(path) ⇒ Boolean
Returns If the loading was successful.
72 73 74 75 76 |
# File 'lib/rubyexts/mixins/import.rb', line 72 def import!(path) path = File.join(self.root, path) file = self.find_absolute(path) Kernel.load(file) end |
#import_first(paths, options = Hash.new) ⇒ Object
60 61 62 63 64 65 66 67 |
# File 'lib/rubyexts/mixins/import.rb', line 60 def import_first(paths, = Hash.new) paths.each do |path| fullpath = self.find_absolute(path) next if fullpath.nil? return self.import(fullpath, ) end raise LoadError unless [:soft] end |