Class: DirectoryMonitor

Inherits:
Object
  • Object
show all
Defined in:
lib/directory_monitor.rb

Instance Method Summary collapse

Constructor Details

#initialize(dir, ops = {}) ⇒ DirectoryMonitor

Returns a new instance of DirectoryMonitor.



2
3
4
5
6
7
# File 'lib/directory_monitor.rb', line 2

def initialize(dir, ops = {})
  @base_dir = dir
  @mtimes = nil
  @ignore = [ops[:ignore]].flatten.compact
  update! if ops[:update]
end

Instance Method Details

#had_changes?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/directory_monitor.rb', line 15

def had_changes?
  @last_result
end

#has_changes?Boolean

Returns:

  • (Boolean)


9
10
11
12
13
# File 'lib/directory_monitor.rb', line 9

def has_changes?
  old_mtimes = @mtimes
  @last_result = old_mtimes != update!
  had_changes?
end

#update!Object



19
20
21
22
23
24
25
26
27
28
29
# File 'lib/directory_monitor.rb', line 19

def update!
  ignore_list = @ignore.map{|ig| Dir.glob(ig)}.flatten.map{|f|File.expand_path f}
  files = Dir.glob(File.join(@base_dir, "**/*")).map{|f|File.expand_path f}
  @mtimes = files.inject({}) do |result, file|
    if ignore_list.include? file
      result
    else
      result.merge( { file => File.mtime(file) } )
    end
  end
end