class AutoReload

Attributes

name[RW]
verbose[RW]

Public Class Methods

new() click to toggle source
# File vendor/qwik/lib/qwik/autoreload.rb, line 20
def initialize
  @status = {}
  @thread = nil
  @verbose = false
  @name = nil
end
start(interval, verbose = false, name=nil) click to toggle source
# File vendor/qwik/lib/qwik/autoreload.rb, line 13
def self.start(interval, verbose = false, name=nil)
  ar = AutoReload.new
  ar.verbose = verbose
  ar.name = name
  ar.autoreload(interval)
end

Public Instance Methods

autoreload(interval) click to toggle source
# File vendor/qwik/lib/qwik/autoreload.rb, line 35
def autoreload(interval)
  @thread = Thread.new {
    loop {
      begin
        update
      rescue Exception
        STDOUT.puts(message('reload: '+$!))
      end
      sleep interval
    }
  }
  @thread.abort_on_exception = true
end
message(str) click to toggle source
# File vendor/qwik/lib/qwik/autoreload.rb, line 29
def message(str)
  msg = str
  msg = @name.to_s+': '+msg if @name
  return msg
end

Private Instance Methods

check_lib(lib) click to toggle source
# File vendor/qwik/lib/qwik/autoreload.rb, line 58
def check_lib(lib)
  if @status[lib]
    file, mtime = @status[lib]
    return if ! FileTest.exist?(file) # file is disappered.
    curtime = File.mtime(file).to_i
    if mtime < curtime
      if @verbose
        $stdout.puts(message("reload: \"#{file}\""))
      end
      load file      # Load it.
      @status[lib] = [file, curtime]
    end
    return
  end

  check_path = [''] + $LOAD_PATH
  #check_path = ['']
  check_path.each {|path|
    file = File.join(path, lib)
    file = lib if path.empty? # Check if the lib is a filename.
    if FileTest.exist?(file)
      @status[lib] = [file, File.mtime(file).to_i]
      return 
    end
  }

  #raise "The library '#{lib}' is not found."
  # $stdout.puts(message("The library '#{lib}' is not found.")) if @verbose
end
get_status(file) click to toggle source
# File vendor/qwik/lib/qwik/autoreload.rb, line 88
def get_status(file)
  if FileTest.exist?(file)
    return [file, File.mtime(file).to_i]
  end
  return nil
end
update() click to toggle source
# File vendor/qwik/lib/qwik/autoreload.rb, line 51
def update
  check_lib = [$0] + $"
  check_lib.each {|lib|
    check_lib(lib)
  }
end