Class: EpubForge::Utils::ClassLoader
- Inherits:
-
FunWith::Files::FilePath
- Object
- FunWith::Files::FilePath
- EpubForge::Utils::ClassLoader
- Defined in:
- lib/utils/class_loader.rb
Overview
filepath string with metadata, representing a class file that can be loaded.
Direct Known Subclasses
Class Method Summary collapse
- .loaded_classes ⇒ Object
- .loaded_directories ⇒ Object
- .namespace(nsp = nil) ⇒ Object
- .require_me(*loadables) ⇒ Object
Instance Method Summary collapse
- #class_loaded? ⇒ Boolean
- #class_name ⇒ Object
-
#require_me ⇒ Object
Returns true if an error was raised when trying to require the file, or if the expected class is not loaded after the file was required.
- #to_class ⇒ Object
Class Method Details
.loaded_classes ⇒ Object
6 7 8 |
# File 'lib/utils/class_loader.rb', line 6 def self.loaded_classes @loaded_classes ||= [] end |
.loaded_directories ⇒ Object
10 11 12 |
# File 'lib/utils/class_loader.rb', line 10 def self.loaded_directories @loaded_directories ||= [] end |
.namespace(nsp = nil) ⇒ Object
14 15 16 17 |
# File 'lib/utils/class_loader.rb', line 14 def self.namespace( nsp = nil ) @namespace = nsp unless nsp.nil? @namespace end |
.require_me(*loadables) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/utils/class_loader.rb', line 57 def self.require_me( *loadables ) @loaded_classes ||= [] @loaded_directories ||= [] for loadable in loadables loadable = self.new( loadable ) if loadable.file? if loadable.require_me @loaded_classes << loadable.to_class else puts "Warning: Failed to load #{loadable.class_name} from file #{loadable}" end elsif loadable.directory? @loaded_directories << loadable loadable.glob( "**", "*.rb" ).each do |entry| self.require_me( entry ) end else puts "Warning: Could not find file #{loadable} to load classes from." end end end |
Instance Method Details
#class_loaded? ⇒ Boolean
43 44 45 46 47 48 49 50 |
# File 'lib/utils/class_loader.rb', line 43 def class_loaded? begin self.to_class return true rescue NameError # There's gotta be another way I should be doing this. return false end end |
#class_name ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/utils/class_loader.rb', line 19 def class_name unless @class_name base = self.basename.to_s.split(".")[0].epf_camelize @class_name = "#{self.class.namespace}::#{base}" end @class_name end |
#require_me ⇒ Object
Returns true if an error was raised when trying to require the file, or if the expected class is not loaded after the file was required. Proper naming is very important here.
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/utils/class_loader.rb', line 30 def require_me begin require self.to_s rescue NameError => e puts "Error encounterd while trying to load #{self.class_name} from #{self}" puts e. puts e.backtrace.map{|line| "\t#{line}" } return false end return self.class_loaded? end |
#to_class ⇒ Object
52 53 54 55 |
# File 'lib/utils/class_loader.rb', line 52 def to_class return @klass unless @klass.nil? @klass = Utils::Misc.constantize( self.class_name ) end |