Module: MagicLoader

Defined in:
lib/magic_loader.rb,
lib/magic_loader/tasks.rb

Overview

Generates the MagicLoader rake task

Defined Under Namespace

Classes: Task

Constant Summary collapse

BEGIN_MAGIC =
"#-----BEGIN MAGICLOADER MAGIC BLOCK-----"
END_MAGIC =
"#------END MAGICLOADER MAGIC BLOCK------"
MAGIC_WARNING =
[
  "# Automagically generated by MagicLoader. Editing may",
  "# result in bad juju. Edit at your own risk!"
]
MAGIC_REGEXP =
/#{BEGIN_MAGIC}.*#{END_MAGIC}/m

Class Method Summary collapse

Class Method Details

.require_all(*args) ⇒ Object

A wonderfully simple way to load your code.

The easiest way to use require_all is to just point it at a directory containing a bunch of .rb files. These files can be nested under subdirectories as well:

MagicLoader.require_all 'lib'

This will find all the .rb files under the lib directory and load them. The proper order to load them in will be determined automatically.

If the dependencies between the matched files are unresolvable, it will throw the first unresolvable NameError.

You can also give it a glob, which will enumerate all the matching files:

MagicLoader.require_all 'lib/**/*.rb'

It will also accept an array of files:

MagicLoader.require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file(f) }

Or if you want, just list the files directly as arguments:

MagicLoader.require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'

Raises:

  • (LoadError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/magic_loader.rb', line 36

def self.require_all(*args)
  # Handle passing an array as an argument
  args.flatten!
  
  if args.size > 1
    # Expand files below directories
    files = args.map do |path|
      if File.directory? path
        Dir[File.join(path, '**', '*.rb')]
      else
        path
      end
    end.flatten
  else
    arg = args.first
    begin
      # Try assuming we're doing plain ol' require compat
      stat = File.stat(arg)
      
      if stat.file?
        files = [arg]
      elsif stat.directory?
        files = Dir.glob File.join(arg, '**', '*.rb')
      else
        raise ArgumentError, "#{arg} isn't a file or directory"
      end
    rescue Errno::ENOENT
      # If the stat failed, maybe we have a glob!
      files = Dir.glob arg
      
      # Maybe it's an .rb file and the .rb was omitted
      if File.file?(arg + '.rb')
        require(arg + '.rb')
        return true
      end
      
      # If we ain't got no files, the glob failed
      raise LoadError, "no such file to load -- #{arg}" if files.empty?
    end
  end

  # If there's nothing to load, you're doing it wrong!
  raise LoadError, "no files to load" if files.empty?
  
  # Sort input files to give semi-deterministic results
  files.sort!
  
  # Store load order as it's calculated
  load_order = []
  
  begin
    failed = []
    first_name_error = nil
    
    # Attempt to load each file, rescuing which ones raise NameError for
    # undefined constants.  Keep trying to successively reload files that 
    # previously caused NameErrors until they've all been loaded or no new
    # files can be loaded, indicating unresolvable dependencies.
    files.each do |file|
      original_constants = Module.constants
      
      begin
        require file
        load_order << file
      rescue NameError => ex
        new_constants = Module.constants - original_constants
        new_constants.each { |name| Object.send(:remove_const, name) }
        
        failed << file
        first_name_error ||= ex
      end
    end
    
    # If this pass didn't resolve any NameErrors, we've hit an unresolvable
    # dependency, so raise one of the exceptions we encountered.
    if failed.size == files.size
      raise first_name_error
    else
      files = failed
    end
  end until failed.empty?
  
  load_order
end