Sha256: 39386c508ec5fe248d1c687df1795db5bed8a18d43b371b9c64491e91db35039

Contents?: true

Size: 1.24 KB

Versions: 1

Compression:

Stored size: 1.24 KB

Contents

module Guard
  class Linux < Listener
    attr_reader :inotify, :files, :latency, :callback
    
    def initialize
      @inotify = INotify::Notifier.new
      @files   = []
      @latency = 0.5
    end
    
    def on_change(&callback)
      @callback = callback
      inotify.watch(Dir.pwd, :recursive, :attrib, :modify, :create) do |event|
        unless event.name == "" # Event on root directory
          @files << event.absolute_name
        end
      end
    end
    
    def start
      @stop = false
      watch_change
    end
    
    def stop
      @stop = true
      inotify.stop
    end
    
    def self.usable?
      require 'rb-inotify'
      if !defined?(INotify::VERSION) || Gem::Version.new(INotify::VERSION) < Gem::Version.new('0.5.1')
        UI.info "Please update rb-inotify (>= 0.5.1)"
        false
      else
        true
      end
    rescue LoadError
      UI.info "Please install rb-inotify gem for Linux inotify support"
      false
    end
    
  private
    
    def watch_change
      while !@stop
        inotify.process
        unless files.empty?
          files.map! { |file| file.gsub("#{Dir.pwd}/", '') }
          callback.call(files)
          files.clear
        end
        sleep latency
      end
    end
    
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
guard-0.2.0.beta.1 lib/guard/listeners/linux.rb