class Ramaze::Reloader

High performant source reloader

This class acts as Rack middleware.

It does not depend on Ramaze itself, but you might have to adjust the Reloader::Hooks module or include your own module to override the hooks. You also might have to set the Log constant.

Currently, it uses RInotify if available and falls back to using File.stat.

Please note that this will not reload files in the background, it does so only when actively called In case of Ramaze it is performing a check/reload cycle at the start of every request, but also respects a cool down time, during which nothing will be done.

After every reload the OPTIONS hash will be checked for changed options and assigned to the instance, so you may change options during the lifetime of your application.

A number of hooks will be executed during the reload cycle, see Ramaze::ReloaderHooks for more information.

Constants

OPTIONS
Watcher

Public Class Methods

new(app) click to toggle source

Creates a new instance of the class and saves the application it has to watch.

@author Michael Fellinger @since 09-08-2008 @param [Ramaze::App] app The application to monitor.

# File lib/ramaze/reloader.rb, line 71
def initialize(app)
  @app = app
  @files = {}
  @watcher = Watcher.new
  options_reload
end

Public Instance Methods

call(env) click to toggle source

Allows this class to be called as a Rack middleware.

@author Michael Fellinger @since 09-08-2008 @param [Hash] env A hash containing all environment details.

# File lib/ramaze/reloader.rb, line 97
def call(env)
  options_reload

  @watcher.call(@cooldown) do
    if @control
      instance_eval(&@control)
    elsif @thread
      Thread.exclusive{ cycle }
    else
      cycle
    end
  end

  @app.call(env)
end
cycle() click to toggle source

Loops through all the files and reloads all changes files.

@author Michael Fellinger @since 09-08-2008

# File lib/ramaze/reloader.rb, line 119
def cycle
  before_cycle

  rotation{|file| @watcher.watch(file) }
  @watcher.changed_files{|f| safe_load(f) }

  after_cycle
end
figure_path(file, paths) click to toggle source

Tries to find a given file in an array of file paths.

@author Michael Fellinger @since 09-08-2008 @param [String] file The name of the file to look for. @param [Array] paths An array of paths to search. @return [String]

# File lib/ramaze/reloader.rb, line 166
def figure_path(file, paths)
  if Pathname.new(file).absolute?
    return File.exist?(file) ? file : nil
  end

  paths.each do |possible_path|
    full_path = File.join(possible_path, file)
    return full_path if File.exist?(full_path)
  end
  nil
end
options_reload() click to toggle source

Returns all the options for this class.

@author Michael Fellinger @since 09-08-2008 @return [Array]

# File lib/ramaze/reloader.rb, line 85
def options_reload
  @cooldown, @ignore, @control, @thread =
    OPTIONS.values_at(:cooldown, :ignore, :control, :thread)
end
rotation() { |path| ... } click to toggle source
# File lib/ramaze/reloader.rb, line 144
def rotation
  files = [$0, __FILE__, *$LOADED_FEATURES].uniq
  paths = ['./', *$LOAD_PATH].uniq

  files.each do |file|
    next if file =~ @ignore
    if not @files.has_key?(file) and path = figure_path(file, paths)
      @files[file] = path
      yield path
    end
  end
end
safe_load(file) click to toggle source

A safe Kernel::load, issuing the hooks depending on the results

@author Michael Fellinger @since 09-08-2008 @param [String] file Path to the file to safely load.

# File lib/ramaze/reloader.rb, line 135
def safe_load(file)
  before_safe_load(file)
  load(file)
  after_safe_load_succeed(file)
rescue Object => ex
  Log.error(ex)
  after_safe_load_failed(file, ex)
end