Class Camping::Reloader
In: lib/camping/reloader.rb
Parent: Object

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.

Methods

apps   clear   new   reload!   update  

Classes and Modules

Class Camping::Reloader::App

Attributes

scripts  [R] 

Public Class methods

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.

[Source]

     # File lib/camping/reloader.rb, line 150
150:     def initialize(*scripts)
151:       @scripts = []
152:       update(*scripts)
153:     end

Public Instance methods

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).

[Source]

     # 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.

[Source]

     # File lib/camping/reloader.rb, line 173
173:     def clear
174:       @scrips = []
175:     end

Simply calls reload! on all the Script objects.

[Source]

     # File lib/camping/reloader.rb, line 178
178:     def reload!
179:       @scripts.each { |script| script.reload! }
180:     end

Updates the reloader to only use the scripts provided:

  reloader.update("examples/blog.rb", "examples/wiki.rb")

[Source]

     # 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

[Validate]