Class: PryReload::Watch
- Inherits:
-
Object
- Object
- PryReload::Watch
- Includes:
- Singleton
- Defined in:
- lib/pry-reload/watch.rb
Constant Summary collapse
- @@mutex =
Mutex.new
Instance Method Summary collapse
- #dirs ⇒ Object
-
#initialize ⇒ Watch
constructor
A new instance of Watch.
- #process ⇒ Object
- #process_event(evt) ⇒ Object
- #reload!(output) ⇒ Object
- #setup ⇒ Object
Constructor Details
#initialize ⇒ Watch
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
#dirs ⇒ Object
17 18 19 |
# File 'lib/pry-reload/watch.rb', line 17 def dirs Dir.glob("**/*/"); end |
#process ⇒ Object
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 |
#setup ⇒ Object
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 |