class Hyperstack::Autoloader

Public Class Methods

const_missing(const_name, mod) click to toggle source
# File lib/hyperstack/autoloader.rb, line 38
def self.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)
rescue Exception => e
  puts "HyperStack autoloader failed attempting to load #{mod}::#{const_name}.  Could be a bug in autoloader"
end
guess_for_anonymous(const_name) click to toggle source
# File lib/hyperstack/autoloader.rb, line 46
def self.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() click to toggle source
# File lib/hyperstack/autoloader.rb, line 9
def self.history
  @@history
end
history=(a) click to toggle source

All files ever loaded.

# File lib/hyperstack/autoloader.rb, line 6
def self.history=(a)
  @@history = a
end
load_missing_constant(from_mod, const_name) click to toggle source
# File lib/hyperstack/autoloader.rb, line 54
def self.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 = underscore(qualified_name)

  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 from_mod.respond_to?(:parent) &&  (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
load_paths() click to toggle source
# File lib/hyperstack/autoloader.rb, line 17
def self.load_paths
  @@load_paths
end
load_paths=(a) click to toggle source
# File lib/hyperstack/autoloader.rb, line 14
def self.load_paths=(a)
  @@load_paths = a
end
loaded() click to toggle source
# File lib/hyperstack/autoloader.rb, line 25
def self.loaded
  @@loaded
end
loaded=(a) click to toggle source
# File lib/hyperstack/autoloader.rb, line 22
def self.loaded=(a)
  @@loaded = a
end
loading() click to toggle source
# File lib/hyperstack/autoloader.rb, line 33
def self.loading
  @@loading
end
loading=(a) click to toggle source
# File lib/hyperstack/autoloader.rb, line 30
def self.loading=(a)
  @@loading = a
end
missing_name?(e, name) click to toggle source
# File lib/hyperstack/autoloader.rb, line 78
def self.missing_name?(e, name)
  mn = if /undefined/ !~ e.message
         $1 if /((::)?([A-Z]\w*)(::[A-Z]\w*)*)$/ =~ e.message
       end
  mn == name
end
qualified_name_for(mod, name) click to toggle source

Returns the constant path for the provided parent and constant name.

# File lib/hyperstack/autoloader.rb, line 86
def self.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) click to toggle source
# File lib/hyperstack/autoloader.rb, line 91
def self.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) click to toggle source
# File lib/hyperstack/autoloader.rb, line 111
def self.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
underscore(string) click to toggle source
# File lib/hyperstack/autoloader.rb, line 136
def self.underscore(string)
  string.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
end