Class: ClassLoader::Watcher

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(monitor) ⇒ Watcher

Returns a new instance of Watcher.



4
5
6
7
8
# File 'lib/class_loader/watcher.rb', line 4

def initialize monitor
  @monitor = monitor
  @paths, @files = [], {}
  @interval = 2
end

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



2
3
4
# File 'lib/class_loader/watcher.rb', line 2

def interval
  @interval
end

#pathsObject

Returns the value of attribute paths.



2
3
4
# File 'lib/class_loader/watcher.rb', line 2

def paths
  @paths
end

Instance Method Details

#checkObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/class_loader/watcher.rb', line 26

def check
  monitor.synchronize do
    paths.each do |path|
      Dir.glob("#{path}/**/*.rb").each do |class_path|
        updated_at = File.mtime class_path
        if last_updated_at = files[class_path]
          if last_updated_at < updated_at
            class_file_name = class_path.sub "#{path}/", ''
            reload class_file_name
            files[class_path] = updated_at
          end
        else
          files[class_path] = updated_at
        end
      end
    end
  end
end

#startObject



16
17
18
19
20
21
22
23
24
# File 'lib/class_loader/watcher.rb', line 16

def start
  return if thread
  @thread = Thread.new do
    while true
      sleep interval
      check
    end
  end
end

#stopObject



10
11
12
13
14
# File 'lib/class_loader/watcher.rb', line 10

def stop
  return unless thread
  thread.kill
  @thread = nil
end