Class: Camping::Reloader

Inherits:
Object show all
Defined in:
lib/camping/reloader.rb

Overview

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.

Defined Under Namespace

Classes: App, Script

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*scripts) ⇒ Reloader

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.



150
151
152
153
# File 'lib/camping/reloader.rb', line 150

def initialize(*scripts)
  @scripts = []
  update(*scripts)
end

Instance Attribute Details

#scriptsObject (readonly)

Returns the value of attribute scripts.



35
36
37
# File 'lib/camping/reloader.rb', line 35

def scripts
  @scripts
end

Instance Method Details

#appsObject

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



185
186
187
188
189
# File 'lib/camping/reloader.rb', line 185

def apps
  @scripts.inject({}) do |hash, script|
    hash.merge(script.apps)
  end
end

#clearObject

Removes all the scripts from the reloader.



173
174
175
# File 'lib/camping/reloader.rb', line 173

def clear
  @scrips = []
end

#reload!Object

Simply calls reload! on all the Script objects.



178
179
180
# File 'lib/camping/reloader.rb', line 178

def reload!
  @scripts.each { |script| script.reload! }
end

#update(*scripts) ⇒ Object

Updates the reloader to only use the scripts provided:

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


158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/camping/reloader.rb', line 158

def update(*scripts)
  old = @scripts.dup
  clear
  @scripts = scripts.map do |script|
    s = Script.new(script)
    if pos = old.index(s)
      # We already got a script, so we use the old (which might got a mtime)
      old[pos]
    else
      s.load_apps
    end
  end
end