public
Last active — forked from nutrun/sinatra_reloader.rb

  • Download Gist
sinatra_reloader.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
# 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

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.