Module: Rails2Preload

Defined in:
lib/rails_2_preload.rb,
lib/rails_2_preload/spin.rb

Overview

The purpose of Rails2Preload is to optimize testing with Spin and Rails 2. It does this by splitting the Rails 2 initialization method (Rails::Initializer#process) into two phases: preload and postload. This gives us the capability of using Spin:

  • without having to reboot Rails every time a class is modified

  • without having to disable class caching

Defined Under Namespace

Modules: Spin

Constant Summary collapse

METHODS =

All of the methods of the Rails initialization process, in order.

[
  :check_ruby_version,
  :install_gem_spec_stubs,
  :set_load_path,
  :add_gem_load_paths,
  :require_frameworks,
  :set_autoload_paths,
  :add_plugin_load_paths,
  :load_environment,
  :preload_frameworks,
  :initialize_encoding,
  :initialize_database,
  :initialize_cache,
  :initialize_framework_caches,
  :initialize_logger,
  :initialize_framework_logging,
  :initialize_dependency_mechanism,
  :initialize_whiny_nils,
  :initialize_time_zone,
  :initialize_i18n,
  :initialize_framework_settings,
  :initialize_framework_views,
  :initialize_metal,
  :add_support_load_paths,
  :check_for_unbuilt_gems,
  :load_gems,
  :load_plugins,
  :add_gem_load_paths,
  :load_gems,
  :check_gem_dependencies,
  :load_application_initializers,
  :after_initialize,
  :initialize_database_middleware,
  :prepare_dispatcher,
  :initialize_routing,
  :load_observers,
  :load_view_paths,
  :load_application_classes,
  :disable_dependency_loading
]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.postload_methodsObject (readonly)

The methods run in the postload phase.



60
61
62
# File 'lib/rails_2_preload.rb', line 60

def postload_methods
  @postload_methods
end

.preload_methodsObject (readonly)

The methods run in the preload phase.



58
59
60
# File 'lib/rails_2_preload.rb', line 58

def preload_methods
  @preload_methods
end

Class Method Details

.postload(initializer) ⇒ Object

Called by the Rails patch to run the postload phase.



81
82
83
# File 'lib/rails_2_preload.rb', line 81

def self.postload(initializer)
  postload_methods.each { |method| benchmark(initializer, method) }
end

.preload(initializer) ⇒ Object

Called by the Rails patch to run the preload phase.



76
77
78
# File 'lib/rails_2_preload.rb', line 76

def self.preload(initializer)
  preload_methods.each { |method| benchmark(initializer, method) }
end

.preload_until(method) ⇒ Object

Given a method name, the methods of the Rails initialization process are split into two groups, preload and postload.

Raises:

  • (ArgumentError)


65
66
67
68
69
70
# File 'lib/rails_2_preload.rb', line 65

def self.preload_until(method)
  index = METHODS.index(method)
  raise(ArgumentError, method) if index.nil?

  @preload_methods, @postload_methods = METHODS[0..index - 1], METHODS[index..-1]
end

.prepare_railsObject

Called by the :before_preload Spin hook to prepare Rails.



86
87
88
89
90
91
# File 'lib/rails_2_preload.rb', line 86

def self.prepare_rails
  # We need to boot Rails before adding methods to Rails::Initializer.
  # Otherwise, Rails.booted? returns true and Rails.boot! short-circuits.
  boot_rails
  apply_initializer_patch
end