Class: PryReload::Watch

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/pry-reload/watch.rb

Constant Summary collapse

@@mutex =
Mutex.new

Instance Method Summary collapse

Constructor Details

#initializeWatch

Returns a new instance of Watch.



10
11
12
13
14
15
# File 'lib/pry-reload/watch.rb', line 10

def initialize
  @notifier = INotify::Notifier.new
  @modified = []
  setup
  process
end

Instance Method Details

#dirsObject



17
18
19
# File 'lib/pry-reload/watch.rb', line 17

def dirs
  Dir.glob("**/*/");
end

#processObject



37
38
39
40
41
42
# File 'lib/pry-reload/watch.rb', line 37

def process
  @thread ||= Thread.new do
    #puts "Running!"
    @notifier.run
  end
end

#process_event(evt) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/pry-reload/watch.rb', line 21

def process_event(evt)
  if File.directory?(evt.absolute_name)
    evt.notifier.watch(evt.absolute_name)
  elsif evt.absolute_name.end_with?(".rb")
    @@mutex.synchronize { @modified << evt.absolute_name }
    #puts "modified #{evt.absolute_name}"
  end
end

#reload!(output) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/pry-reload/watch.rb', line 44

def reload!(output)
  @@mutex.synchronize do
    if @modified.size == 0
      output.puts "Nothing changed!"
    else
      changed = @modified.dup.uniq
      @modified = []
      while path = changed.shift
        output.puts "Reloading #{path}"
        load path
      end
    end
  end
end

#setupObject



30
31
32
33
34
35
# File 'lib/pry-reload/watch.rb', line 30

def setup
  dirs.each do |dir|
    #puts "Listening #{dir}"
    @notifier.watch(dir, :modify, &Proc.new { |evt| process_event(evt) })
  end
end