A couple of handy features to enhance Kernel#autoload.
Changes since 0.0.1: minor tweaks to the gem, and migrated to gemcutter.
Question: How do I add a choice of dependencies? Right now, I can use
either
extlib or activesupport for inflections.
Also: This is my first ever ruby-talk announcement. Someone please tell
me if
I am Doing It Wrong.
Pasted from the README:
Features
- Fire Kernel#autoload at an entire directory
- Fire it again at a subdirectory, for namespace’d models
Motivation/Examples
I was using Ramaze as a web framework, and discovered that the standard
Ramaze
way of loading a directory of files is to slurp them all (with
‘acquire’). I
decided I’d rather use autoload, and not load things I’m not using. But
I
wasn’t really looking forward to this:
autoload :User, ‘model/user’
autoload :Clan, ‘model/clan’
autoload :Item, ‘model/item’
…ad nauseum. So, the first feature is a shallow, directory-based
autoload:
AutoLoader << ‘model’
Of course, being a pedant, I like to namespace my models. I don’t want
to
slurp the entire directory tree. Instead, you can do this:
class User < MyFavoriteORM::Model
include AutoLoader
…
end
Now, all AutoLoaded paths will be searched for a subdirectory called
‘user’,
and all files within will be autoloaded. Just as if you’d done:
User.autoload :Anonymous, ‘model/user/anonymous’
User.autoload :Registered, ‘model/user/registered’
Of course, there’s no requirement that you have a base class, or
anything of
the sort. If you just want a namespace, you can always do:
module MyNamespace; include AutoLoader; end
And you’re done.
Compatibility
Currently, Merb’s extlib and Rails’ activesupport are supported. It
should be
trivial to interface with other libraries.