Sweetens up your autoloading of classes and modules
MIT License
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
:)
Insert in your Gemfile:
gem 'sweetloader'
Run bundler to install
$bundle$
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.
The following global config vars are available in SweetLoader:
Note: AutoLoader is available as a deprecated alias for SweetLoader (versions < 1.5)
Set a specific root which the dir will be calculated relative to, using root.
SweetLoader.root = 'fixtures'
Now the constant ::CanTango
will be resolved as 'fixtures/can_tango'
Normally the constant CanTango will be translated to the dir 'can_tango'
, here we override this so it will instead be translated to 'cantango'
.
SweetLoader.namespaces= {:CanTango => 'cantango', :Permithelper => 'permit_helper'}
SweetLoader 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.
You can also specify the SweetLoader options directly as an option hash on the #autoload_modules
call:
sweetload :Configuration, :mode => :require
sweetload :Configuration, :Factory, :root => 'helpers'
sweetload :Configuration, :Factory, :Permit, :ns => {:CanTango => 'cantango'} # or use :namespaces
Note: #autoload_modules
is deprecated in place of #sweetload
This option is only available for autoload_modules
. It can be used to execute more general substitution logic on the generated file path:
sweetload :Configuration, :Factory, :Permit, :mutate_path => Proc.new {|path| path.sub(/(ies)/, 'y') }
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!
sweet_scope
:ns => { :AutoloadModules => 'fixtures/autoload_modulez'},
:mutate_path => Proc.new {|path| path.sub(/(ies)/, 'y') } do
sweetload :Admin
end
sweetload :Editor
end
end
Note: #autoload_scope
is deprecated in place of #sweet_scope
Sweetloader also encludes a convenient extension called ClassExt
, which can be used to find a Module or Class based on a name.
module First
module Blip
def self.blip
end
end
end
module Second
include_and_extend ClassExt
include_and_extend First
end
Second.new.try_module('Blip').blip
Second.try_module('Blip').blip
Second.try_class('Blip') == nil # => ClassNotFoundError
Second.find_first_class(:Blop, :blip_blop, 'Blip') # => ClassNotFoundError
Second.find_first_module('Blop', :blip) # => Blip:Class
Copyright (c) 2011 Kristian Mandrup. See LICENSE.txt for further details.