Skip to content

waves/autocode

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

= 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.

About

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

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%