Skip to content
Browse files

updated readme about load_all, load_rel, autoload_all and autoload_rel

  • Loading branch information...
1 parent 5191296 commit bbdb0eba3e36b1dde513585dcf2dabdb557a35eb @jarmo committed Sep 14, 2010
Showing with 57 additions and 8 deletions.
  1. +7 −0 CHANGES
  2. +47 −5 README.textile
  3. +3 −3 lib/require_all.rb
View
7 CHANGES
@@ -1,3 +1,10 @@
+1.2.0:
+
+* Add load_all, and load_rel which behave similarly to require_all/require_rel except that Kernel#load is used
+* Add autoload_all and autoload_rel (see README and/or specs for examples of usage)
+* Minor bug fixes
+* Improved specs
+
1.1.0:
* Add require_rel (require_all relative to the current file)
View
52 README.textile
@@ -51,9 +51,49 @@ Even better, require_rel still has the full power of require_all, so you can
use require_rel to load entire directories of code too. If "foobar" is a
directory this will load all the .rb files found under that directory with
automagic dependency handling.
+
+Also load_all and load_rel methods exist to use Kernel#load instead of Kernel#require!
It's just that easy! Code loading shouldn't be hard.
+h2. autoload_all
+
+There's also a methods for performing autoloading - what a bargain!
+Similar syntax is used as for require and load methods although some things have to be
+kept in mind:
+
+* Directory and file names have to reflect namespaces and/or constant names - e.g.
+ a file called my_file.rb in directories dir1/dir2 has to be defined like this:
+ <code>
+ module Dir1
+ module Dir2
+ class MyFile
+ # ...
+ end
+ end
+ end
+
+ # in a loader.rb, which is in a parent directory for dir1:
+ autoload_all File.dirname(__FILE__) + "/dir1"
+ </code>
+
+* A :base_dir option has to be specified if loading directories or files from some other location
+ than top-level directory:
+ <code>
+ # in dir1/other_file.rb
+ autoload_all File.dirname(__FILE__) + "/dir2/my_file.rb",
+ :base_dir => File.dirname(__FILE__) + "/../dir1" # top-level namespace starts from dir1
+ </code>
+
+* All namespaces will be created dynamically by autoload_all - this means that defined?(Dir1) will
+ return "constant" even if my_file.rb is not loaded!
+
+Of course there's also an autoload_rel method:
+ <code>autoload_rel "dir2/my_file.rb", :base_dir => File.dirname(__FILE__) + "/../dir1"</code>
+
+If having some problems with autoload_all or autoload_rel then set $DEBUG to true to see how files
+are mapped to their respective modules and classes.
+
h2. Methodology
I didn't invent the approach this gem uses. It was shamelessly stolen from
@@ -70,14 +110,16 @@ Merb (which apparently stole it from elsewhere). Here's how it works:
h2. Questions? Comments? Concerns?
-You can reach the author on github or freenode: "tarcieri"
+You can reach the author on github or freenode: "jarm0"
-Or by email: "tony@medioh.com":mailto:tony@medioh.com
+Or by email: "jarmo.p@gmail.com":mailto:jarmo.p@gmail.com
-Got issues with require_all to report? Post 'em here:
+Got issues with require_all to report? Post 'em here:
-"Github Tracker":http://github.com/tarcieri/require_all/issues
+"Github Tracker":http://github.com/jarmo/require_all/issues
h2. License
-MIT (see the LICENSE file for details)
+require_all was done originally by Tony Arcieri who asked me to maintain the gem.
+
+MIT (see the LICENSE file for details)
View
6 lib/require_all.rb
@@ -175,7 +175,7 @@ def load_rel(*paths)
# Performs Kernel#autoload on all of the files rather than requiring immediately.
#
# Note that all Ruby files inside of the specified directories should have same module name as
- # the directory itself and file names should reflect the class names.
+ # the directory itself and file names should reflect the class/module names.
# For example if there is a my_file.rb in directories dir1/dir2/ then
# there should be a declaration like this in my_file.rb:
# module Dir1
@@ -189,7 +189,7 @@ def load_rel(*paths)
# If the filename and namespaces won't match then my_file.rb will be loaded into wrong module!
# Better to fix these files.
#
- # Set $DEBUG=true to see how files will be autoloaded if experiencing problems.
+ # Set $DEBUG=true to see how files will be autoloaded if experiencing any problems.
#
# If trying to perform autoload on some individual file or some inner module, then you'd have
# to always specify *:base_dir* option to specify where top-level namespace resides.
@@ -201,7 +201,7 @@ def load_rel(*paths)
# :base_dir => File.dirname(__FILE__) + '/dir1'
#
# WARNING: All modules will be created even if files themselves aren't loaded yet, meaning
- # that all code which depends of the modules being loaded or not will not work, like usages
+ # that all the code which depends of the modules being loaded or not will not work, like usages
# of define? and it's friends.
#
# Also, normal caveats of using Kernel#autoload apply - you have to remember that before

0 comments on commit bbdb0eb

Please sign in to comment.
Something went wrong with that request. Please try again.