Parent

Namespace

Camping::Reloader

The Camping Reloader

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.

Wrapping Your Apps

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.

Attributes

scripts[R]
apps[R]

Public Class Methods

new(*scripts) click to toggle source

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
139:     def initialize(*scripts)
140:       @scripts = []
141:       @apps = {}
142:       update(*scripts)
143:     end

Public Instance Methods

clear() click to toggle source

Removes all the scripts from the reloader.

     # File lib/camping/reloader.rb, line 166
166:     def clear
167:       @scripts = []
168:       @apps = {}
169:     end
on_reload(&blk) click to toggle source
     # File lib/camping/reloader.rb, line 145
145:     def on_reload(&blk)
146:       @callback = blk
147:     end
reload!() click to toggle source

Simply calls reload! on all the Script objects.

     # File lib/camping/reloader.rb, line 181
181:     def reload!
182:       @apps = {}
183:       @scripts.each do |script|
184:         script.reload!
185:         @apps.update(script.apps)
186:       end
187:     end
script(app) click to toggle source

Returns the script which provided the given app.

     # File lib/camping/reloader.rb, line 172
172:     def script(app)
173:       @scripts.each do |script|
174:         return script if script.apps.values.include?(app)
175:       end
176:       
177:       false
178:     end
update(*scripts) click to toggle source

Updates the reloader to only use the scripts provided:

  reloader.update("examples/blog.rb", "examples/wiki.rb")
     # File lib/camping/reloader.rb, line 152
152:     def update(*scripts)
153:       old_scripts = @scripts.dup
154:       clear
155:       
156:       @scripts = scripts.map do |script|
157:         file = File.expand_path(script)
158:         old_scripts.detect { |s| s.file == file } or
159:         Script.new(script, @callback)
160:       end
161:       
162:       reload!
163:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.