Class: Build::Files::Monitor

Inherits:
Object
  • Object
show all
Defined in:
lib/build/files/monitor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMonitor

Returns a new instance of Monitor.



28
29
30
31
32
# File 'lib/build/files/monitor.rb', line 28

def initialize
  @directories = Hash.new { |hash, key| hash[key] = Set.new }
  
  @updated = false
end

Instance Attribute Details

#updatedObject (readonly)

Returns the value of attribute updated.



34
35
36
# File 'lib/build/files/monitor.rb', line 34

def updated
  @updated
end

Instance Method Details

#add(handle) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/build/files/monitor.rb', line 70

def add(handle)
  handle.directories.each do |directory|
    @directories[directory] << handle

    # We just added the first handle:
    if @directories[directory].size == 1
      # If the handle already existed, this might trigger unnecessarily.
      @updated = true
    end
  end
    
  handle
end

#delete(handle) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/build/files/monitor.rb', line 51

def delete(handle)
  handle.directories.each do |directory|
    @directories[directory].delete(handle)

    # Remove the entire record if there are no handles:
    if @directories[directory].size == 0
      @directories.delete(directory)
  
      @updated = true
    end
  end
end

#rootsObject



47
48
49
# File 'lib/build/files/monitor.rb', line 47

def roots
  @directories.keys
end

#run(options = {}, &block) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/build/files/monitor.rb', line 84

def run(options = {}, &block)
  default_driver = case RUBY_PLATFORM
    when /linux/i; :inotify
    when /darwin/i; :fsevent
    else; :polling
  end
  
  if driver = options.fetch(:driver, default_driver)
    method_name = "run_with_#{driver}"
    Files.send(method_name, self, options, &block)
  end
end

#track_changes(files, &block) ⇒ Object



64
65
66
67
68
# File 'lib/build/files/monitor.rb', line 64

def track_changes(files, &block)
  handle = Handle.new(self, files, &block)
    
  add(handle)
end

#update(directories, *args) ⇒ Object

Notify the monitor that files in these directories have changed.



37
38
39
40
41
42
43
44
45
# File 'lib/build/files/monitor.rb', line 37

def update(directories, *args)
  directories.each do |directory|
    # directory = File.realpath(directory)
    
    @directories[directory].each do |handle|
      handle.changed!(*args)
    end
  end
end