Class: Sinatra::Reloader::FileWatcher

Inherits:
Array
  • Object
show all
Extended by:
Enumerable
Defined in:
lib/sinatra/reloader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file) ⇒ FileWatcher

Returns a new instance of FileWatcher.



39
40
41
42
43
# File 'lib/sinatra/reloader.rb', line 39

def initialize(file)
  @reload, @file = true, file
  @mtime = File.exist?(file) ? File.mtime(file) : Time.at(0)
  super()
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



13
14
15
# File 'lib/sinatra/reloader.rb', line 13

def file
  @file
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



13
14
15
# File 'lib/sinatra/reloader.rb', line 13

def mtime
  @mtime
end

Class Method Details

.each(&block) ⇒ Object



35
36
37
# File 'lib/sinatra/reloader.rb', line 35

def self.each(&block)
  @map.values.each(&block) 
end

.new(file) ⇒ Object Also known as: []



22
23
24
25
26
27
28
29
# File 'lib/sinatra/reloader.rb', line 22

def self.new(file)
  file = file.expand_path
  begin
    file = file.realpath
  rescue Errno::ENOENT
  end
  @map[file] ||= super
end

.register(route) ⇒ Object



18
19
20
# File 'lib/sinatra/reloader.rb', line 18

def self.register(route)
  new(route.file) << route if route.file?
end

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/sinatra/reloader.rb', line 45

def changed?
  !File.exist? file or @mtime != File.mtime(file)
end

#dont_reload!(dont = true) ⇒ Object



49
50
51
# File 'lib/sinatra/reloader.rb', line 49

def dont_reload!(dont = true)
  @reload = !dont
end

#reloadObject



57
58
59
# File 'lib/sinatra/reloader.rb', line 57

def reload
  reload! if reload?
end

#reload!Object



61
62
63
64
65
66
67
68
69
# File 'lib/sinatra/reloader.rb', line 61

def reload!
  each { |route| route.deactivate }
  $LOADED_FEATURES.delete file
  clear
  if File.exist? file
    @mtime = File.mtime(file)
    require file
  end
end

#reload?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/sinatra/reloader.rb', line 53

def reload?
  @reload and changed?
end