Class: Opal::Autoloader
- Defined in:
- lib/opal/autoloader.rb,
lib/opal/autoloader/version.rb
Constant Summary collapse
- VERSION =
'0.1.0'
Class Method Summary collapse
- .add_load_path(path) ⇒ Object
- .const_missing(const_name, mod) ⇒ Object
- .guess_for_anonymous(const_name) ⇒ Object
-
.history ⇒ Object
All modules ever loaded.
- .init ⇒ Object
- .load_missing_constant(from_mod, const_name) ⇒ Object
- .missing_name?(e, name) ⇒ Boolean
-
.qualified_name_for(mod, name) ⇒ Object
Returns the constant path for the provided parent and constant name.
- .require_or_load(from_mod, module_path) ⇒ Object
- .search_for_module(path) ⇒ Object
-
.to_constant_name(desc) ⇒ Object
Convert the provided const desc to a qualified constant name (as a string).
Class Method Details
.add_load_path(path) ⇒ Object
11 12 13 |
# File 'lib/opal/autoloader.rb', line 11 def add_load_path(path) @load_paths << path end |
.const_missing(const_name, mod) ⇒ Object
22 23 24 25 26 |
# File 'lib/opal/autoloader.rb', line 22 def const_missing(const_name, mod) # name.nil? is testing for anonymous from_mod = mod.name.nil? ? guess_for_anonymous(const_name) : mod load_missing_constant(from_mod, const_name) end |
.guess_for_anonymous(const_name) ⇒ Object
28 29 30 31 32 33 34 |
# File 'lib/opal/autoloader.rb', line 28 def guess_for_anonymous(const_name) if Object.const_defined?(const_name) raise NameError.new "#{const_name} cannot be autoloaded from an anonymous class or module", const_name else Object end end |
.history ⇒ Object
All modules ever loaded.
7 8 9 |
# File 'lib/opal/autoloader.rb', line 7 def history @history end |
.init ⇒ Object
15 16 17 18 19 20 |
# File 'lib/opal/autoloader.rb', line 15 def init @history = [] @load_paths = Set.new @loaded = Set.new @loading = [] end |
.load_missing_constant(from_mod, const_name) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/opal/autoloader.rb', line 36 def load_missing_constant(from_mod, const_name) # see active_support/dependencies.rb in case of reloading on how to handle qualified_name = qualified_name_for(from_mod, const_name) qualified_path = qualified_name.underscore module_path = search_for_module(qualified_path) if module_path if @loading.include?(module_path) raise "Circular dependency detected while autoloading constant #{qualified_name}" else require_or_load(from_mod, module_path) raise LoadError, "Unable to autoload constant #{qualified_name}, expected #{module_path} to define it" unless from_mod.const_defined?(const_name, false) return from_mod.const_get(const_name) end elsif (parent = from_mod.parent) && parent != from_mod && ! from_mod.parents.any? { |p| p.const_defined?(const_name, false) } begin return parent.const_missing(const_name) rescue NameError => e raise unless missing_name?(e, qualified_name_for(parent, const_name)) end end end |
.missing_name?(e, name) ⇒ Boolean
60 61 62 63 64 65 |
# File 'lib/opal/autoloader.rb', line 60 def missing_name?(e, name) mn = if /undefined/ !~ e. $1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ e. end mn == name end |
.qualified_name_for(mod, name) ⇒ Object
Returns the constant path for the provided parent and constant name.
68 69 70 71 |
# File 'lib/opal/autoloader.rb', line 68 def qualified_name_for(mod, name) mod_name = to_constant_name(mod) mod_name == 'Object' ? name.to_s : "#{mod_name}::#{name}" end |
.require_or_load(from_mod, module_path) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/opal/autoloader.rb', line 73 def require_or_load(from_mod, module_path) return if @loaded.include?(module_path) @loaded << module_path @loading << module_path begin result = require module_path rescue Exception @loaded.delete module_path raise LoadError, "Unable to autoload: require_or_load #{module_path} failed" ensure @loading.pop end # Record history *after* loading so first load gets warnings. @history << module_path result # end end |
.search_for_module(path) ⇒ Object
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/opal/autoloader.rb', line 93 def search_for_module(path) # oh my! imagine Bart Simpson, writing on the board: # "javascript is not ruby, javascript is not ruby, javascript is not ruby, ..." # then running home, starting irb, on the fly developing a chat client and opening a session with Homer at his workplace: "Hi Dad ..." @load_paths.each do |load_path| mod_path = load_path + '/' + path return mod_path if `Opal.modules.hasOwnProperty(#{mod_path})` end return path if `Opal.modules.hasOwnProperty(#{path})` nil # Gee, I sure wish we had first_match ;-) end |
.to_constant_name(desc) ⇒ Object
Convert the provided const desc to a qualified constant name (as a string). A module, class, symbol, or string may be provided.
107 108 109 110 111 112 113 114 115 116 |
# File 'lib/opal/autoloader.rb', line 107 def to_constant_name(desc) #:nodoc: case desc when String then desc.sub(/^::/, '') when Symbol then desc.to_s when Module desc.name || raise(ArgumentError, 'Anonymous modules have no name to be referenced by') else raise TypeError, "Not a valid constant descriptor: #{desc.inspect}" end end |