Class: QuickServe::Rails::Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/quick_serve/rails/listener.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeListener

constructor



13
14
15
# File 'lib/quick_serve/rails/listener.rb', line 13

def initialize
  self.interval = 2 # decrease the CPU load by increasing the interval
end

Instance Attribute Details

#filesObject

Returns the value of attribute files.



10
11
12
# File 'lib/quick_serve/rails/listener.rb', line 10

def files
  @files
end

#intervalObject

Returns the value of attribute interval.



10
11
12
# File 'lib/quick_serve/rails/listener.rb', line 10

def interval
  @interval
end

Instance Method Details

#check_filesObject

check files to find these that have changed



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/quick_serve/rails/listener.rb', line 33

def check_files
  updated = []
  files.each do |filename, mtime| 
    begin
      current_mtime = File.stat(filename).mtime
    rescue Errno::ENOENT
      # file was not found and was probably deleted
      # remove the file from the file list 
      files.delete(filename)
      next
    end
    if current_mtime != mtime  
      updated << filename
      # update the mtime in file registry so we it's only send once
      files[filename] = current_mtime
      puts "quick_serve: spotted change in #{filename}"
    end
  end
  QuickServe::Rails::Snapshot.reset if updated != []
  false
end

#find_filesObject

Find the files to process, ignoring temporary files, source configuration management files, etc., and return a Hash mapping filename to modification time. source: ZenTest/autotest.rb



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/quick_serve/rails/listener.rb', line 60

def find_files
  result = {}
  targets = ['app'] # start simple
  targets.each do |target|
    order = []
    Find.find(target) do |f|
      next if test ?d, f
      next if f =~ /(swp|~|rej|orig)$/ # temporary/patch files
      next if f =~ /(\.svn|\.git)$/ # subversion/git
      next if f =~ /\/\.?#/ # Emacs autosave/cvs merge files
      filename = f.sub(/^\.\//, '')

      result[filename] = File.stat(filename).mtime rescue next
    end
  end

  self.files = result
end

#startObject

find files and start the listener



18
19
20
21
22
23
24
# File 'lib/quick_serve/rails/listener.rb', line 18

def start
  puts "** quick_serve: scanning for files... "
  # build a file collection
  find_files
  puts "** quick_serve: watching #{files.size} files for changes... "
  wait  
end

#waitObject

wait for a specified interval and check files for changes source: ZenTest/autotest.rb



28
29
30
# File 'lib/quick_serve/rails/listener.rb', line 28

def wait
  Kernel.sleep self.interval until check_files
end