Class | Camping::Reloader |
In: |
lib/camping/reloader.rb
|
Parent: | Object |
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.
scripts | [R] |
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 150 150: def initialize(*scripts) 151: @scripts = [] 152: update(*scripts) 153: end
Returns a Hash of all the apps available in the scripts, where the key would be the name of the app (the one you gave to Camping.goes) and the value would be the app (wrapped inside App).
# File lib/camping/reloader.rb, line 185 185: def apps 186: @scripts.inject({}) do |hash, script| 187: hash.merge(script.apps) 188: end 189: end
Removes all the scripts from the reloader.
# File lib/camping/reloader.rb, line 173 173: def clear 174: @scrips = [] 175: end
Updates the reloader to only use the scripts provided:
reloader.update("examples/blog.rb", "examples/wiki.rb")
# File lib/camping/reloader.rb, line 158 158: def update(*scripts) 159: old = @scripts.dup 160: clear 161: @scripts = scripts.map do |script| 162: s = Script.new(script) 163: if pos = old.index(s) 164: # We already got a script, so we use the old (which might got a mtime) 165: old[pos] 166: else 167: s.load_apps 168: end 169: end 170: end