T
Tony Arcieri
require_all
A wonderfully simple way to load your code.
http://github.com/tarcieri/require_all/tree/master
Tired of futzing around with require statements everywhere, littering your
code with require File.dirname(__FILE__) crap? What if you could just point
something at a big directory full of code and have everything just
automagically load regardless of the dependency structure?
Wouldn=92t that be nice? Well, now you can!
require 'require_all'
You can now require_all in a multitude of different ways:
require_all *args
One of the easiest ways to require_all is to give it a glob, which will
enumerate all the matching files and load them in the proper order. For
example, to load all the Ruby files under the =91lib=92 directory, just do:
require_all 'lib/**/*.rb'
If the dependencies between the matched files are unresolvable, it will
throw the first unresolvable NameError.
Don=92t want to give it a glob? Just give it a list of files:
require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file(f) }
Or if you want, just list the files directly as arguments:
require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'
It=92s just that easy! Code loading shouldn=92t be hard.
Methodology
I didn=92t invent the approach this gem uses. It was shamelessly stolen fro=
m
Merb. Once upon a time at MountainWest RubyConf we were discussing how
horrible ActiveSupport=92s dependencies.rb hijacking of const_missing and
someone described the approach Merb used to me. It was so simple and clean!
Here=92s how it works:
1. Enumerate the files in the glob
2. Try to load all of the files. If we encounter a NameError loading a
particular file, store that file in a =93try to load it later=94 list.
3. If all the files loaded, great, we=92re done! If not, go through the
=93try to load it later=94 list again rescuing NameErrors the same way.
4. If we walk the whole =93try to load it later=94 list and it doesn=92t=
shrink
at all, we=92ve encountered an unresolvable dependency. In this case,
require_all will rethrow the first NameError it encountered.
Questions? Comments? Concerns?
You can reach the author on github or freenode: =93tarcieri=94
Or by email: (e-mail address removed)
Got issues with require_all to report? Post =91em here:
Github Tracker <http://github.com/tarcieri/require_all/issues>
License
MIT (see the LICENSE file for details)
--=20
Tony Arcieri
medioh.com
A wonderfully simple way to load your code.
http://github.com/tarcieri/require_all/tree/master
Tired of futzing around with require statements everywhere, littering your
code with require File.dirname(__FILE__) crap? What if you could just point
something at a big directory full of code and have everything just
automagically load regardless of the dependency structure?
Wouldn=92t that be nice? Well, now you can!
require 'require_all'
You can now require_all in a multitude of different ways:
require_all *args
One of the easiest ways to require_all is to give it a glob, which will
enumerate all the matching files and load them in the proper order. For
example, to load all the Ruby files under the =91lib=92 directory, just do:
require_all 'lib/**/*.rb'
If the dependencies between the matched files are unresolvable, it will
throw the first unresolvable NameError.
Don=92t want to give it a glob? Just give it a list of files:
require_all Dir.glob("blah/**/*.rb").reject { |f| stupid_file(f) }
Or if you want, just list the files directly as arguments:
require_all 'lib/a.rb', 'lib/b.rb', 'lib/c.rb', 'lib/d.rb'
It=92s just that easy! Code loading shouldn=92t be hard.
Methodology
I didn=92t invent the approach this gem uses. It was shamelessly stolen fro=
m
Merb. Once upon a time at MountainWest RubyConf we were discussing how
horrible ActiveSupport=92s dependencies.rb hijacking of const_missing and
someone described the approach Merb used to me. It was so simple and clean!
Here=92s how it works:
1. Enumerate the files in the glob
2. Try to load all of the files. If we encounter a NameError loading a
particular file, store that file in a =93try to load it later=94 list.
3. If all the files loaded, great, we=92re done! If not, go through the
=93try to load it later=94 list again rescuing NameErrors the same way.
4. If we walk the whole =93try to load it later=94 list and it doesn=92t=
shrink
at all, we=92ve encountered an unresolvable dependency. In this case,
require_all will rethrow the first NameError it encountered.
Questions? Comments? Concerns?
You can reach the author on github or freenode: =93tarcieri=94
Or by email: (e-mail address removed)
Got issues with require_all to report? Post =91em here:
Github Tracker <http://github.com/tarcieri/require_all/issues>
License
MIT (see the LICENSE file for details)
--=20
Tony Arcieri
medioh.com