Module: Sinatra::Reloader

Defined in:
lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/reloader.rb

Overview

Sinatra::Reloader

<b>DEPRECATED:<b> Please consider using an alternative like rerun or rack-unreloader instead.

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 reloader 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 reloader 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

Using the Reloader in Other Environments

By default, the reloader is only enabled for the development environment. Similar to registering the reloader in a modular application, a classic application requires manually enabling the extension for it to be available in a non-development environment.

require "sinatra"
require "sinatra/reloader"

configure :production do
  enable :reloader
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. You can also use after_reload to execute a block after any file being reloaded.

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'
after_reload do
  puts 'reloaded'
end

# 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'
    after_reload do
      puts 'reloaded'
    end
  end

  # Your modular application code goes here...
end

Defined Under Namespace

Modules: BaseMethods, ExtensionMethods Classes: Watcher

Constant Summary collapse

MUTEX_FOR_PERFORM =
Mutex.new
@@after_reload =

Allow a block to be executed after any file being reloaded

[]

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.perform(klass) ⇒ Object

Reloads the modified files, adding, updating and removing the needed elements.



247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/reloader.rb', line 247

def self.perform(klass)
  reloaded_paths = []
  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) }
    # Deletes all old elements.
    watcher.elements.delete_if { true }
    $LOADED_FEATURES.delete(watcher.path)
    require watcher.path
    watcher.update
    reloaded_paths << watcher.path
  end
  return if reloaded_paths.empty?

  @@after_reload.each do |block|
    block.arity.zero? ? block.call : block.call(reloaded_paths)
  end
  # Prevents after_reload from increasing each time it's reloaded.
  @@after_reload.delete_if do |blk|
    path, = blk.source_location
    path && reloaded_paths.include?(path)
  end
end

.registered(klass) ⇒ Object

When the extension is registered 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.



227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/reloader.rb', line 227

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?
      MUTEX_FOR_PERFORM.synchronize { Reloader.perform(klass) }
    end
  end
  klass.set(:inline_templates, klass.app_file) if klass == Sinatra::Application
end

Instance Method Details

#after_reload(&block) ⇒ Object



220
221
222
# File 'lib/rubypitaya/app-template/vendor/bundle/ruby/3.1.0/gems/sinatra-contrib-3.0.5/lib/sinatra/reloader.rb', line 220

def after_reload(&block)
  @@after_reload << block
end