class Abstractive::Refrescar

Public Class Methods

new(options={}) click to toggle source
# File lib/abstractive/refrescar.rb, line 7
def initialize(options={})
  @running = false
  @logger = options[:logger]
  options[:root] ||= Dir.pwd
  @root = Array[options[:root]]
  @debug = options.fetch(:debug, false)
  @announcing = options.fetch(:announcing, true)
  @reschedule = options.fetch(:reschedule, false)
  @after_reload = options.fetch(:after_reload, false)
  @watcher = INotify::Notifier.new
  @events = [
    :close_write,
    #de :modify         #de This seems to fire twice in certain cases. Used :close_write instead.
  ]
  start if options.fetch(:autostart, true)
end

Public Instance Methods

add(root, relative=nil) click to toggle source
# File lib/abstractive/refrescar.rb, line 24
def add(root, relative=nil)
  raise "Already running." if @running
  root = File.expand_path(root, relative) if relative
  @root << root
  set_watchers(root)
end
reload(file) click to toggle source
# File lib/abstractive/refrescar.rb, line 67
def reload(file)
  begin
    load(file)
    console("Reloaded: #{file}") if @announcing
      @watcher.watch(file, *@events) { reload file } if @reschedule
      @after_reload.call(file) if @after_reload.is_a? Proc
  rescue SyntaxError => ex
    exception(ex, "Code Reloading > Syntax error in #{file}", console: false)
  rescue LoadError => ex
    exception(ex, "Code Reloading > Missing file: #{file}", console: false)
  end
rescue => ex
  exception(ex, "Trouble reloading file: #{file}", console: false)
end
reloading() click to toggle source
# File lib/abstractive/refrescar.rb, line 35
def reloading
  @root.each { |root| set_watchers(root) }
  debug("Started code reloading...") if @debug
  @watcher.run
rescue => ex
  exception(ex, "Trouble running reloader.")
  raise
end
running?() click to toggle source
# File lib/abstractive/refrescar.rb, line 31
def running?
  @running === true
end
set_watchers(path) click to toggle source
# File lib/abstractive/refrescar.rb, line 49
def set_watchers(path)
  debug("Watching path: #{path}") if @debug
  Find.find( path ) { |file|
    if !File.extname(file) == '.rb' and !File.directory? file
      Find.prune
    else
      begin
        if File.extname(file) == '.rb'
          debug("Will reload: #{file}") if @debug
          @watcher.watch(file, *@events) { reload(file) }
        end
      rescue => ex
        exception(ex, "Code Reloading > Trouble setting watchers.")
      end
    end
  }
end
start() click to toggle source
# File lib/abstractive/refrescar.rb, line 44
def start
  async.reloading
  @running = true
end