class AutoReload::Reloader

Reload class does all the heavy lifting for AutoReload library.

Constants

DEFAULT_INTERVAL

Public: Default interval is one second.

Attributes

files[R]

Public: List of files provided to autoreload.

interval[R]

Public: The periodic interval of reload in seconds.

status[R]

Public: Status hash, used to track reloads.

thread[R]

Public: References the reload thread.

Public Class Methods

new(options={}, &block) click to toggle source

New Reloader.

options - The Hash options used to refine the reloader (default: {}):

:interval - Seconds between updates.
:verbose  - True provides reload warning.
:reprime  - Include $0 in reload list.
# File lib/autoreload/reloader.rb, line 25
def initialize(options={}, &block)
  @interval   = (options[:interval] || DEFAULT_INTERVAL).to_i
  @verbose    = (options[:verbose])
  @reprime    = (options[:reprime])
  @watch_gems = (options[:watch_gems])

  @status   = {}

  features = $".dup
  if block
    block.call
    @files = ($" - features).select do |f|
      (@watch_gems ? true : f.start_with?(Dir.pwd)) && f.end_with?(".rb")
    end.reverse

    if @verbose
      puts "watching files:"
      puts "---------------"
      puts @files
      puts "---------------"
    end
  else
    @files = []
  end
end
start(*args, &block) click to toggle source

Public: Shortcut for Reloader.new(*args).start.

# File lib/autoreload/reloader.rb, line 11
def self.start(*args, &block)
  self.new(*args, &block).start
end

Public Instance Methods

reprime?() click to toggle source

Public: Put $0 in the reload list?

Returns true/false whether to include $0.

# File lib/autoreload/reloader.rb, line 95
def reprime?
  @reprime
end
start() click to toggle source

Public: Start the reload thread.

# File lib/autoreload/reloader.rb, line 52
def start
  update # prime the path loads
  @thread = Thread.new do
    loop do
      begin
        update
      rescue Exception
        warn "autoreload failed unexpectedly: #{$!}"
      end
      sleep @interval
    end
  end
  @thread.abort_on_exception = true
  @thread
end
stop() click to toggle source

Public: Kills the autoreload thread.

# File lib/autoreload/reloader.rb, line 69
def stop
  @thread.kill if @thread
end
verbose?() click to toggle source

Public: Provide warning on reload.

Returns true/false if versboe mode.

# File lib/autoreload/reloader.rb, line 88
def verbose?
  @verbose || $VERBOSE
end

Private Instance Methods

check(lib) click to toggle source

Check status and reload if out-of-date.

We can't check mtime under 1.8 b/c $LOADED_FEATURES does not store the full path.

lib - A library file.

Returns Array of [file, mtime].

# File lib/autoreload/reloader.rb, line 125
def check(lib)
  if RUBY_VERSION < '1.9'
    warn "reload: '#{lib}'" if verbose?
    begin
      load lib
    rescue LoadError
      # file has been removed
    end
  else
    file, mtime = @status[lib]
    if file
      if FileTest.exist?(file)
        curtime = File.mtime(file).to_i
        if mtime < curtime
          warn "reload: '#{file}'" if verbose?
          load file
          @status[lib] = [file, curtime]
        end
      else
        # file has been removed
      end
    else
      @status[lib] = get_status(lib)
    end
  end
end
get_status(file) click to toggle source

Get library file status.

file - The file path from which to get status.

Returns Array of [file, mtime] or [nil, nil] if file not found.

# File lib/autoreload/reloader.rb, line 157
def get_status(file)
  if FileTest.exist?(file)
    [file, File.mtime(file).to_i]
  else
    warn "reload fail: library '#{file}' not found" if verbose?
    #raise "The library '#{file}' is not found."
    #$stdout.puts(message("The library '#{file}' is not found.")) if @verbose
    [nil, nil]
  end
end
libraries() click to toggle source

The library files to autoreload.

# File lib/autoreload/reloader.rb, line 104
def libraries
  if @files.empty?
    @reprime ? [$0] + $" : $"
  else
    @files
  end
end
update() click to toggle source

Iterate through all selection library files and reload if needed.

# File lib/autoreload/reloader.rb, line 113
def update
  libraries.each{ |lib| check(lib) }
end