Module: AutoLoader

Defined in:
lib/autoloader.rb

Constant Summary collapse

EXTENSIONS =
%w{rb o}.map(&:freeze).freeze

Class Method Summary collapse

Class Method Details

.<<(path) ⇒ Object



61
62
63
64
65
66
67
68
69
70
# File 'lib/autoloader.rb', line 61

def self.<< path
  unless self.paths.include? path
    self.paths << path
    unless $:.include? path
      $: << path
    end
    
    self.update(:path => path)
  end
end

._try_methods(object, *methods) ⇒ Object

Find a method that the object responds to, and execute that. If no methods match, load dependencies and try again.



25
26
27
28
29
30
31
32
# File 'lib/autoloader.rb', line 25

def self._try_methods object, *methods
  method = methods.find {|m| object.respond_to? m}
  if method
    object.send method
  else
    nil
  end
end

.included(mod) ⇒ Object

Some syntactic sugar



102
103
104
# File 'lib/autoloader.rb', line 102

def self.included mod
  self.update(:parent => mod)
end

.own_dependenciesObject

We need some sort of inflection library. Note: Sequel provides its own inflector, too… This should be done in a more extensible way.



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/autoloader.rb', line 11

def self.own_dependencies
  require 'extlib/string'
rescue LoadError
  begin
    require 'active_support/inflector'
  rescue LoadError
    require 'rubygems'
    gem 'activesupport'
    require 'active_support/inflector'
  end
end

.pathsObject



51
52
53
# File 'lib/autoloader.rb', line 51

def self.paths
  @paths ||= Set.new
end

.push(*args) ⇒ Object



55
56
57
58
59
# File 'lib/autoloader.rb', line 55

def self.push *args
  args.each do
    self << args
  end
end

.to_const_name(string) ⇒ Object



46
47
48
# File 'lib/autoloader.rb', line 46

def self.to_const_name string
  self.try_methods string, :to_const_string, :underscore
end

.to_file_name(string) ⇒ Object

Semi-agnostic inflections. Send patches, feel free to monkeypatch it, or define your own camelize/underscore methods.



43
44
45
# File 'lib/autoloader.rb', line 43

def self.to_file_name string
  self.try_methods string, :to_const_path, :camelize
end

.try_methods(object, *methods) ⇒ Object



33
34
35
36
37
38
# File 'lib/autoloader.rb', line 33

def self.try_methods object, *methods
  self._try_methods(object, *methods) || (
    self.own_dependencies
    self._try_methods(object, *methods) || raise(NoMethodError, methods.first.to_s)
  )
end

.update(options) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/autoloader.rb', line 72

def self.update(options)
  if !options[:path]
    self.paths.each {|p| self.update(options.merge(:path => p))}
  else
    
    path = Pathname.new options[:path]
    parent = options[:parent]
    
    glob_path = parent.nil? ? path : path.join(self.to_file_name parent.name)
    
    EXTENSIONS.each do |ext|
      Pathname.glob(glob_path.join("*.#{ext}").to_s).each do |file|
        basename = file.basename(file.extname)
        
        camelized = self.to_const_name basename.to_s
        next unless camelized =~ /^[A-Z]\w*/
        
        req_path = file.parent.relative_path_from(path).join(basename).to_s
        
        if parent.nil?
          Object.autoload camelized, req_path
        else
          parent.autoload camelized, req_path
        end
      end
    end
  end
end