Sweetloader

A 'sweet' autoloader designed to autoload your modules and classes more easily, by taking advantage of a standard directory structure approach for the placement of your classes and modules. You can also configure Sweetloader to override the defaults in various ways... and even have it use require instead of autoload :)

Install

Insert in your Gemfile:

gem 'sweetloader'

Run bundler to install

$bundle$

Usage

Simply use the autoload_modules macro in your Module and Class definitions.

CanTango
  autoload_module :Configuration, :Factory, :Permit
end

Each of these modules are then expected to be in @catango/configuration.rb@ and similar relative to a load path.

AutoLoader Configuration

The following global config vars are available in AutoLoader:

  • root
  • namespaces
  • mode

Root

Set a specific root which the dir will be calculated relative to, using root.

AutoLoader.root = 'fixtures'

Now the constant ::CanTango will be resolved as 'fixtures/can_tango'

Namespaces

Normally the constant CanTango will be translated to the dir 'can_tango', here we override this so it will instead be translated to 'cantango'.

AutoLoader.namespaces= {:CanTango => 'cantango', :Permithelper => 'permit_helper'}

Modes

AutoLoader supports the following modes: :autoload, :require

The mode :require is used to execute require statements directly. The :autoload mode is used to do autoloading. Note that autoloading is not stable in multi-threaded environments and will likely be deprecated in later version of Ruby. However autoloading does perform lazy loading and can be advantageous when you want to speed up initial load time and don't run in a multi-threaded environment.

autoload_modules Configuration

You can also specify the AutoLoader options directly as an option hash on the #autoload_modules call:

  autoload_modules :Configuration, :mode => :require
  autoload_modules :Configuration, :Factory, :root => 'helpers'
  autoload_modules :Configuration, :Factory, :Permit, :ns => {:CanTango => 'cantango'} # or use :namespaces

Mutate path option

This option is only available for autoload_modules. It can be used to execute more general substitution logic on the generated file path:

  autoload_modules :Configuration, :Factory, :Permit, :mutate_path => Proc.new {|path| path.sub(/(ies)/, 'y') }

Scopes

A Scope allows options to be specified that are effective on all autoload_modules statements within the scope.

module AutoloadModules
  module Configuration
    # the scope options only have effect for autoload_modules statements within it!
    autoload_scope 
        :ns => { :AutoloadModules => 'fixtures/autoload_modulez'},
        :mutate_path => Proc.new {|path| path.sub(/(ies)/, 'y') } do
      autoload_modules :Admin
    end

    autoload_modules :Editor
  end
end

Contributing to sweetloader

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright (c) 2011 Kristian Mandrup. See LICENSE.txt for further details.