Home > delayed_load

delayed_load

Delayed_load is a project mainly written in Ruby, based on the MIT license.

Cleanly delay loading of libraries in a rails application

= Delayed Load

== Overview

Delayed Load allows to cleanly delay loading of libraries,

== Installation

As a rails plugin:

script/plugin install git://github.com/p/delayed_load.git

== Usage

Please also see Important Caveat and Working Example sections below.

  1. Create an initializer:

    DelayedLoad.create :foo do require 'foo_library'

    Foo.some_option = :value end

  2. Optionally, add more configuration:

    DelayedLoad.configure :foo do Foo.another_option = :value end

  3. Perform all accumulated work:

    DelayedLoad.load_foo!

== Important Caveat

All of the calls to DelayedLoad should be done from within +after_initialize+ blocks. This is due to the order in which things are loaded by rails, which is as follows:

  1. config/environment.rb
  2. config/environments/#{RAILS_ENV}.rb
  3. vendor/plugins/*/init.rb
  4. config/initializers/*.rb
  5. +after_initialize+

What the above order means is that it is impossible to use DelayedLoad without putting it in an +after_initialize+ block in either config/environment.rb or config/environments/#{RAILS_ENV}.rb as plugins are not yet loaded at that time; and furthermore, because DelayedLoad.configure must be done after DelayedLoad.create for a particular initializer, and DelayedLoad.create is done in +after_initialize+, any configuration done in config/initializers/*.rb must also be put in +after_initialize+ blocks.

== Working Example

In practice, the following code is typically required:

=== config/environment.rb

Rails::Initializer.run do |config|

Other config

# ...

config.after_initialize do
  # First, create an initializer called foo.
  DelayedLoad.create :foo do
    # Here we just require the library.
    require 'foo_library'
  end
end

end

=== config/environment/production.rb

config.after_initialize do

Perform configuration of foo after it is loaded.

DelayedLoad.configure :foo do
  # Set an environment-specific option.
  Foo.option = :value
end

end

=== config/initializers/foo.rb

Need to call out to Rails.configuration for after_initialize here.

Rails.configuration.after_initialize do

Perform configuration of foo after it is loaded.

DelayedLoad.configure :sass do
  # Set an option that is common to all environments here.
  Foo.another_option = :value
end

end

== Advanced Usage

DelayedLoad.create may be called without a block. In such case it will create the initializer with no configuration. DelayedLoad.configure can then be called to add configuration, as follows:

DelayedLoad.create :foo

...

DelayedLoad.configure :foo do Foo.option = :value end

== License

Delayed Load is licensed under the MIT license, as described in included MIT-LICENSE file.

Previous:CallHelp