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

#initialize(logger: nil) ⇒ Monitor

Returns a new instance of Monitor.



59
60
61
62
63
64
65
66
67
# File 'lib/build/files/monitor.rb', line 59

def initialize(logger: nil)
  @directories = Hash.new { |hash, key| hash[key] = Set.new }
  
  @updated = false
  
  @deletions = nil
  
  @logger = logger || Logger.new(nil)
end

Instance Attribute Details

#updatedObject (readonly)

Returns the value of attribute updated.



69
70
71
# File 'lib/build/files/monitor.rb', line 69

def updated
  @updated
end

Instance Method Details

#add(handle) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/build/files/monitor.rb', line 108

def add(handle)
  @logger.debug{"Adding handle: #{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

#default_driverObject



124
125
126
127
128
129
130
# File 'lib/build/files/monitor.rb', line 124

def default_driver
  case RUBY_PLATFORM
  when /linux/i then :inotify
  when /darwin/i then :fsevent
  else :polling
  end
end

#delete(handle) ⇒ Object



93
94
95
96
97
98
99
100
# File 'lib/build/files/monitor.rb', line 93

def delete(handle)
  if @deletions
    @logger.debug{"Delayed delete handle: #{handle}"}
    @deletions << handle
  else
    purge(handle)
  end
end

#rootsObject



89
90
91
# File 'lib/build/files/monitor.rb', line 89

def roots
  @directories.keys
end

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



132
133
134
135
136
137
138
139
# File 'lib/build/files/monitor.rb', line 132

def run(options = {}, &block)
  if driver = (options[:driver] || default_driver)
    method_name = "run_with_#{driver}"
    Files.send(method_name, self, options, &block)
  else
    raise ArgumentError.new("Could not find driver for platform #{RUBY_PLATFORM}!")
  end
end

#track_changes(files, &block) ⇒ Object



102
103
104
105
106
# File 'lib/build/files/monitor.rb', line 102

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.



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/build/files/monitor.rb', line 72

def update(directories, *args)
  @logger.debug{"Update: #{directories} #{args.inspect}"}
  
  delay_deletions do
    directories.each do |directory|
      @logger.debug{"Directory: #{directory}"}
      
      @directories[directory].each do |handle|
        @logger.debug{"Handle changed: #{handle.inspect}"}
        
        # Changes here may not actually require an update to the handle:
        handle.changed!(*args)
      end
    end
  end
end