Skip to content

Instantly share code, notes, and snippets.

@nutrun
Created June 24, 2010 00:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nutrun/450814 to your computer and use it in GitHub Desktop.
Save nutrun/450814 to your computer and use it in GitHub Desktop.
Code reloading for Sinatra
# For "Classic" style/top-level type of apps do something like:
#
# configure :development do
# require File.join(File.dirname(__FILE__), 'sinatra_reloader')
# set :reload_paths, [File.join(File.dirname(__FILE__), '**', '*.rb')]
# end
#
# For "Modular" style/Sinatra::Base subclasses:
#
# configure :development do
# require File.join(File.dirname(__FILE__), 'sinatra_reloader')
# register Sinatra::Reloader
# set :reload_paths, [File.join(File.dirname(__FILE__), '**', '*.rb')]
# end
#
# :reload_paths should be an array of path patterns where the
# source code intended for reloading resides.
# If not set, default is '<sinatra_reloader_dir>/**/*.rb'
module Sinatra
module Reloader
def self.extended(target)
target.before { target.reload }
default_path = File.expand_path(File.join(File.dirname(__FILE__), '**', '*.rb'))
target.set(:reload_paths, [default_path])
end
def reload
source_files = reload_paths.inject({}) do |paths, pattern|
Dir[pattern].each { |path| paths[path] = File.mtime(path) }
paths
end
return if source_files == @source_files
@source_files = source_files
routes.clear
@source_files.keys.each { |path| load(path) }
end
end
register Reloader
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment