Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Ruby library for auto-loading and auto-creation of classes and modules.

branch: master

This branch is 0 commits ahead and 0 commits behind master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 test
Octocat-spinner-32 HISTORY
Octocat-spinner-32 LICENSE
Octocat-spinner-32 README
Octocat-spinner-32 Rakefile
Octocat-spinner-32 autocode.gemspec
README
= Introducing AutoCode 

AutoCode is a class (and module) loader from Ruby, complete with initialization hooks and reloading. It works as a mixin and uses #const_remove for unloading so there can never be any artifacts from prior loads hanging around.

  require 'autocode'
  
  module Application
    include AutoCode
    auto_load true, :directories => [ :configurations, :models, :views, :controllers ]
  end
  
This will attempt to load code dynamically from the given directories, using the module name to determine which directory to look in. Thus, <tt>Application::CustomerModel</tt> could load the file <tt>models/customer_model.rb</tt>.

== Reloading Code

Auto-loaded or created code is automatically reloadable (very useful when debugging running processes). Just call reload on any given module.
  
 *Important*: Only code loaded via *AutoCode* (auto_load or auto_create) will be reloaded.

== Autocreation

Sometimes it's useful to generate defaults for classes or modules that don't exist. This can be particularly powerful when used in combination with auto_loading.

  require 'auto_code'

  module Application
    include AutoCode
    
    %w( Models Views Controllers ).each |name| do
      auto_create_module name do 
        include AutoCode
        auto_load true, :directories => [ name.downcase ]
      end
    end
  end

This will auto_create the modules Configurations, Models, Views, and Controllers within the Application module, as they are referenced, and then initialize them so that they will automatically load source files from the corresponding directories.

For example, referencing <tt>Application::Models::Customer</tt> will cause the file <tt>models/customer.rb</tt> to be loaded.

== Initializing 

Sometimes you want to reopen a class or module and add or change methods, etc. However, in the case of auto-loading or creating unless your code is in an auto_loaded file or an auto_create block, it will get clobbered upon reload. You avoid the clobber using auto_eval, which registers blocks against constant names so that auto_create and auto_load can run them after object creation.

require 'auto_code'

module Application
  include AutoCode
  auto_eval :views do
    auto_eval :error do
      def not_found; "404 Not Found"; end
    end
  end
end

== Other Uses

Autoloading and autocreation, along with reloading, can be used to provide sophisticated rules for loading, generating, and configuring modules and classes within a given module. These capabilities are increasingly found within frameworks like Rails and Camping, but *AutoCode* makes it possible to mixin these capabilities into any situation and precisely control how they are applied.

== Support

Direct questions or comments to the Waves support forum (which also supports AutoCode, as it is principally used within Waves) at http://groups.google.com/group/rubywaves.  You can submit bugs to the Waves issue tracker at http://waves.lighthouseapp.com.

(c) 2008 Dan Yoder

Licensed under the MIT License.
Something went wrong with that request. Please try again.