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, app) ⇒ FileWatcher

Returns a new instance of FileWatcher.



36
37
38
39
40
# File 'lib/sinatra/reloader.rb', line 36

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

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



11
12
13
# File 'lib/sinatra/reloader.rb', line 11

def app
  @app
end

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/sinatra/reloader.rb', line 11

def file
  @file
end

#inline_templatesObject (readonly)

Returns the value of attribute inline_templates.



11
12
13
# File 'lib/sinatra/reloader.rb', line 11

def inline_templates
  @inline_templates
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



11
12
13
# File 'lib/sinatra/reloader.rb', line 11

def mtime
  @mtime
end

Class Method Details

.each(&block) ⇒ Object



32
33
34
# File 'lib/sinatra/reloader.rb', line 32

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

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



19
20
21
22
23
24
25
26
# File 'lib/sinatra/reloader.rb', line 19

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

.register(route) ⇒ Object



15
16
17
# File 'lib/sinatra/reloader.rb', line 15

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

Instance Method Details

#changed?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/sinatra/reloader.rb', line 42

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

#dont_reload!(dont = true) ⇒ Object



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

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

#inline_templates!Object



62
63
64
# File 'lib/sinatra/reloader.rb', line 62

def inline_templates!
  @inline_templates = true
end

#inline_templates?Boolean

Returns:

  • (Boolean)


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

def inline_templates?
  !!inline_templates
end

#reloadObject



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

def reload
  reload! if reload?
end

#reload!Object



66
67
68
69
70
71
72
73
74
75
# File 'lib/sinatra/reloader.rb', line 66

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

#reload?Boolean

Returns:

  • (Boolean)


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

def reload?
  @reload and changed?
end