Class: VResource::FileSystemReloader

Inherits:
Object
  • Object
show all
Defined in:
lib/vresource/file_system_reloader.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directories = Dir.glob("**/lib"), interval = 2) ⇒ FileSystemReloader

Returns a new instance of FileSystemReloader.



5
6
7
8
# File 'lib/vresource/file_system_reloader.rb', line 5

def initialize directories = Dir.glob("**/lib"), interval = 2
  @files = {}
  self.directories, self.interval = directories, interval      
end

Instance Attribute Details

#directoriesObject

Returns the value of attribute directories.



3
4
5
# File 'lib/vresource/file_system_reloader.rb', line 3

def directories
  @directories
end

#intervalObject

Returns the value of attribute interval.



3
4
5
# File 'lib/vresource/file_system_reloader.rb', line 3

def interval
  @interval
end

#threadObject

Returns the value of attribute thread.



3
4
5
# File 'lib/vresource/file_system_reloader.rb', line 3

def thread
  @thread
end

Instance Method Details

#checkObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vresource/file_system_reloader.rb', line 30

def check
  reset if @files.empty?
  
  begin
    check_for_changed_files.each do |type, klass, res|                        
      VResource.notify_observers :update_resource, type, klass, res
    end              
  rescue Exception => e
    warn e
  end
end

#remember_file(path) ⇒ Object



26
27
28
# File 'lib/vresource/file_system_reloader.rb', line 26

def remember_file path
  @files[path] = File.mtime(path)
end

#resetObject



42
43
44
45
46
47
# File 'lib/vresource/file_system_reloader.rb', line 42

def reset
  @files = {}
  all_files.each do |path|
    remember_file path
  end
end

#startObject



10
11
12
13
14
15
16
17
# File 'lib/vresource/file_system_reloader.rb', line 10

def start
  self.thread = Thread.new do        
    while true
      sleep interval
      check
    end
  end
end

#stopObject



19
20
21
22
23
24
# File 'lib/vresource/file_system_reloader.rb', line 19

def stop
  if thread
    thread.kill
    self.thread = nil
  end
end