public
Description: Rubinius, the Ruby VM
Homepage: http://rubini.us
Clone URL: git://github.com/evanphx/rubinius.git
rubinius / kernel / core / autoload.rb
100644 56 lines (48 sloc) 1.069 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
##
# Used to implement Module#autoload.
 
class Autoload
  attr_reader :name
  attr_reader :scope
  attr_reader :path
  attr_reader :original_path
 
  def initialize(name, scope, path)
    @name = name
    @scope = scope
    @original_path = path
    @path, = __split_path__(path)
    Autoload.add(self)
  end
 
  ##
  # When any code that finds a constant sees an instance of Autoload as its match,
  # it calls this method on us
  def call
    require(path)
    scope.const_get(name)
  end
 
  ##
  # Called by Autoload.remove
  def discard
    scope.__send__(:remove_const, name)
  end
 
  ##
  # Class methods
  class << self
    ##
    # Initializes as a Hash with an empty array as the default value
    def autoloads
      @autoloads ||= Hash.new {|h,k| h[k] = Array.new }
    end
 
    ##
    # Called by Autoload#initialize
    def add(al)
      autoloads[al.path] << al
    end
 
    ##
    # Called by require; see kernel/core/compile.rb
    def remove(path)
      al = autoloads.delete(path)
      return unless al
      al.each {|a| a.discard }
    end
  end
end