Camping apps are generally small and predictable. Many Camping apps are contained within a single file. Larger apps are split into a handful of other Ruby libraries within the same directory.
Since Camping apps (and their dependencies) are loaded with Ruby’s require method, there is a record of them in $LOADED_FEATURES. Which leaves a perfect space for this class to manage auto-reloading an app if any of its immediate dependencies changes.
Since bin/camping and the Camping::Server class already use the Reloader, you probably don’t need to hack it on your own. But, if you’re rolling your own situation, here’s how.
Rather than this:
require 'yourapp'
Use this:
require 'camping/reloader' reloader = Camping::Reloader.new('/path/to/yourapp.rb') blog = reloader.apps[:Blog] wiki = reloader.apps[:Wiki]
The blog and wiki objects will behave exactly like your Blog and Wiki, but they will update themselves if yourapp.rb changes.
You can also give Reloader more than one script.
Creates the reloader, assigns a script to it and initially loads the application. Pass in the full path to the script, otherwise the script will be loaded relative to the current working directory.
# File lib/camping/reloader.rb, line 139 def initialize(*scripts) @scripts = [] @apps = {} update(*scripts) end
Removes all the scripts from the reloader.
# File lib/camping/reloader.rb, line 166 def clear @scripts = [] @apps = {} end
# File lib/camping/reloader.rb, line 145 def on_reload(&blk) @callback = blk end
Simply calls reload! on all the Script objects.
# File lib/camping/reloader.rb, line 181 def reload! @apps = {} @scripts.each do |script| script.reload! @apps.update(script.apps) end end
Returns the script which provided the given app.
# File lib/camping/reloader.rb, line 172 def script(app) @scripts.each do |script| return script if script.apps.values.include?(app) end false end
Updates the reloader to only use the scripts provided:
reloader.update("examples/blog.rb", "examples/wiki.rb")
# File lib/camping/reloader.rb, line 152 def update(*scripts) old_scripts = @scripts.dup clear @scripts = scripts.map do |script| file = File.expand_path(script) old_scripts.detect { |s| s.file == file } or Script.new(script, @callback) end reload! end
Generated with the Darkfish Rdoc Generator 2.