Class: Ramaze::Reloader::WatchStat

Inherits:
Object
  • Object
show all
Defined in:
lib/ramaze/reloader/watch_stat.rb

Instance Method Summary collapse

Constructor Details

#initializeWatchStat

Returns a new instance of WatchStat.



4
5
6
7
8
# File 'lib/ramaze/reloader/watch_stat.rb', line 4

def initialize
  # @files[file_path] = stat
  @files = {}
  @last = Time.now
end

Instance Method Details

#call(cooldown) ⇒ Object



10
11
12
13
14
15
# File 'lib/ramaze/reloader/watch_stat.rb', line 10

def call(cooldown)
  if cooldown and Time.now > @last + cooldown
    yield
    @last = Time.now
  end
end

#changed_filesObject

return files changed since last call



40
41
42
43
44
45
46
47
48
49
# File 'lib/ramaze/reloader/watch_stat.rb', line 40

def changed_files
  @files.each do |file, stat|
    if new_stat = safe_stat(file)
      if new_stat.mtime > stat.mtime
        @files[file] = new_stat
        yield(file)
      end
    end
  end
end

#closeObject

no need for cleanup



36
37
# File 'lib/ramaze/reloader/watch_stat.rb', line 36

def close
end

#remove_watch(file) ⇒ Object

stop watching a file for changes



31
32
33
# File 'lib/ramaze/reloader/watch_stat.rb', line 31

def remove_watch(file)
  @files.delete(file)
end

#safe_stat(file) ⇒ Object



51
52
53
54
55
# File 'lib/ramaze/reloader/watch_stat.rb', line 51

def safe_stat(file)
  File.stat(file)
rescue Errno::ENOENT, Errno::ENOTDIR
  nil
end

#watch(file) ⇒ Object

start watching a file for changes true if succeeded, false if failure



19
20
21
22
23
24
# File 'lib/ramaze/reloader/watch_stat.rb', line 19

def watch(file)
  return true if watching?(file) # if already watching
  if stat = safe_stat(file)
    @files[file] = stat
  end
end

#watching?(file) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ramaze/reloader/watch_stat.rb', line 26

def watching?(file)
  @files.has_key?(file)
end