Module: Sinatra::Reloader
- Defined in:
- lib/sinatra/reloader.rb
Overview
Sinatra::Reloader
Extension to reload modified files. Useful during development, since it will automatically require files defining routes, filters, error handlers and inline templates, with every incoming request, but only if they have been updated.
Usage
Classic Application
To enable the realoader in a classic application all you need to do is require it:
require "sinatra"
require "sinatra/reloader" if development?
# Your classic application code goes here...
Modular Application
To enable the realoader in a modular application all you need to do is require it, and then, register it:
require "sinatra/base"
require "sinatra/reloader"
class MyApp < Sinatra::Base
configure :development do
register Sinatra::Reloader
end
# Your modular application code goes here...
end
Changing the Reloading Policy
You can refine the reloading policy with also_reload
and dont_reload
, to customize which files should, and should not, be reloaded, respectively.
Classic Application
Simply call the methods:
require "sinatra"
require "sinatra/reloader" if development?
also_reload '/path/to/some/file'
dont_reload '/path/to/other/file'
# Your classic application code goes here...
Modular Application
Call the methods inside the configure
block:
require "sinatra/base"
require "sinatra/reloader"
class MyApp < Sinatra::Base
configure :development do
register Sinatra::Reloader
also_reload '/path/to/some/file'
dont_reload '/path/to/other/file'
end
# Your modular application code goes here...
end
Defined Under Namespace
Modules: BaseMethods, ExtensionMethods Classes: Watcher
Class Method Summary collapse
-
.perform(klass) ⇒ Object
Reloads the modified files, adding, updating and removing the needed elements.
-
.registered(klass) ⇒ Object
When the extension is registed it extends the Sinatra application
klass
with the modulesBaseMethods
andExtensionMethods
and defines a before filter toperform
the reload of the modified files. -
.thread_safe? ⇒ Boolean
Indicates whether or not we can and need to run thread-safely.
Class Method Details
.perform(klass) ⇒ Object
Reloads the modified files, adding, updating and removing the needed elements.
217 218 219 220 221 222 223 224 225 |
# File 'lib/sinatra/reloader.rb', line 217 def self.perform(klass) Watcher::List.for(klass).updated.each do |watcher| klass.set(:inline_templates, watcher.path) if watcher.inline_templates? watcher.elements.each { |element| klass.deactivate(element) } $LOADED_FEATURES.delete(watcher.path) require watcher.path watcher.update end end |
.registered(klass) ⇒ Object
When the extension is registed it extends the Sinatra application klass
with the modules BaseMethods
and ExtensionMethods
and defines a before filter to perform
the reload of the modified files.
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/sinatra/reloader.rb', line 194 def self.registered(klass) @reloader_loaded_in ||= {} return if @reloader_loaded_in[klass] @reloader_loaded_in[klass] = true klass.extend BaseMethods klass.extend ExtensionMethods klass.set(:reloader) { klass.development? } klass.set(:reload_templates) { klass.reloader? } klass.before do if klass.reloader? if Reloader.thread_safe? Thread.exclusive { Reloader.perform(klass) } else Reloader.perform(klass) end end end end |
.thread_safe? ⇒ Boolean
Indicates whether or not we can and need to run thread-safely.
228 229 230 |
# File 'lib/sinatra/reloader.rb', line 228 def self.thread_safe? Thread and Thread.list.size > 1 and Thread.respond_to?(:exclusive) end |