Camping::Base

Camping::Base is built into each controller by way of the generic routing class Camping::R. In some ways, this class is trying to do too much, but it saves code for all the glue to stay in one place. Forgivable, considering that it’s only really a handful of methods and accessors.

Everything in this module is accessible inside your controllers.

Constants

T
L

Attributes

env[RW]
request[RW]
root[RW]
input[RW]
cookies[RW]
state[RW]
status[RW]
headers[RW]
body[RW]

Public Instance Methods

lookup(n) click to toggle source

Finds a template, returning either:

  false             # => Could not find template
  true              # => Found template in Views
  instance of Tilt  # => Found template in a file
     # File lib/camping-unabridged.rb, line 268
268:     def lookup(n)
269:       T.fetch(n.to_sym) do |k|
270:         t = Views.method_defined?(k) ||
271:           (t = O[:_t].keys.grep(/^#{n}\./)[0]and Template[t].new{O[:_t][t]}) ||
272:           (f = Dir[[O[:views] || "views", "#{n}.*"]*'/'][0]) &&
273:           Template.new(f, O[f[/\.(\w+)$/, 1].to_sym] || {})
274:         
275:         O[:dynamic_templates] ? t : T[k] = t
276:       end
277:     end
mab(&b) click to toggle source

You can directly return HTML from your controller for quick debugging by calling this method and passing some Markaby to it.

  module Nuts::Controllers
    class Info
      def get; mab{ code @headers.inspect } end
    end
  end

You can also pass true to use the :layout HTML wrapping method

     # File lib/camping-unabridged.rb, line 312
312:     def mab(&b)
313:       (@mab ||= Mab.new({},self)).capture(&b)
314:     end
r(s, b, h = {}) click to toggle source

A quick means of setting this controller’s status, body and headers based on a Rack response:

  r(302, 'Location' => self / "/view/12", '')
  r(*another_app.call(@env))

You can also switch the body and the header if you want:

  r(404, "Could not find page")

See also: #, # and #

     # File lib/camping-unabridged.rb, line 327
327:     def r(s, b, h = {})
328:       b, h = h, b if Hash === b
329:       @status = s
330:       @headers.merge!(h)
331:       @body = b
332:     end
r404(p) click to toggle source

Called when a controller was not found. You can override this if you want to customize the error page:

  module Nuts
    def r404(path)
      @path = path
      render :not_found
    end
  end
     # File lib/camping-unabridged.rb, line 361
361:     def r404(p)
362:       P % "#{p} not found"
363:     end
r500(k,m,e) click to toggle source

Called when an exception is raised. However, if there is a parse error in Camping or in your application’s source code, it will not be caught.

k is the controller class, m is the request method (GET, POST, etc.) and e is the Exception which can be mined for useful info.

Be default this simply re-raises the error so a Rack middleware can handle it, but you are free to override it here:

  module Nuts
    def r500(klass, method, exception)
      send_email_alert(klass, method, exception)
      render :server_error
    end
  end  
     # File lib/camping-unabridged.rb, line 380
380:     def r500(k,m,e)
381:       raise e
382:     end
r501(m) click to toggle source

Called if an undefined method is called on a controller, along with the request method m (GET, POST, etc.)

     # File lib/camping-unabridged.rb, line 386
386:     def r501(m)
387:       P % "#{m.upcase} not implemented"
388:     end
redirect(*a) click to toggle source

Formulate a redirect response: a 302 status with Location header and a blank body. Uses Helpers#URL to build the location from a controller route or path.

So, given a root of localhost:3301/articles:

  redirect "view/12"  # redirects to "//localhost:3301/articles/view/12"
  redirect View, 12   # redirects to "//localhost:3301/articles/view/12"

NOTE: This method doesn’t magically exit your methods and redirect. You’ll need to return redirect(...) if this isn’t the last statement in your code, or throw :halt if it’s in a helper.

See: Controllers

     # File lib/camping-unabridged.rb, line 348
348:     def redirect(*a)
349:       r(302,'','Location'=>URL(*a).to_s)
350:     end
render(v, *a, &b) click to toggle source

Display a view, calling it by its method name v. If a layout method is found in Camping::Views, it will be used to wrap the HTML.

  module Nuts::Controllers
    class Show
      def get
        @posts = Post.find :all
        render :index
      end
    end
  end
     # File lib/camping-unabridged.rb, line 291
291:     def render(v, *a, &b)
292:       if t = lookup(v)
293:         r, @_r = @_r, o = Hash === a[1] ? a.pop : {}
294:         s = (t == true) ? mab{ send(v, *a, &b) } : t.render(self, o[:locals] || {}, &b)
295:         s = render(L, o.merge(L => false)) { s } if o[L] or o[L].nil? && lookup(L) && (!r && v.to_s[0] != __)
296:         s
297:       else
298:         raise "no template: #{v}"
299:       end
300:     end
serve(p, c) click to toggle source

Serves the string c with the MIME type of the filename p.

     # File lib/camping-unabridged.rb, line 391
391:     def serve(p, c)
392:       t = Rack::Mime.mime_type(p[/\..*$/], nil) and @headers['Content-Type'] = t
393:       c
394:     end
service(*a) click to toggle source

All requests pass through this method before going to the controller. Some magic in Camping can be performed by overriding this method.

     # File lib/camping-unabridged.rb, line 439
439:     def service(*a)
440:       r = catch(:halt){send(@method, *a)}
441:       @body ||= r 
442:       self
443:     end
to_a() click to toggle source

Turn a controller into a Rack response. This is designed to be used to pipe controllers into the r method. A great way to forward your requests!

  class Read < '/(\d+)'
    def get(id)
      Post.find(id)
    rescue
      r *Blog.get(:NotFound, @headers.REQUEST_URI)
    end
  end
     # File lib/camping-unabridged.rb, line 407
407:     def to_a
408:       @env['rack.session'] = Hash[@state]
409:       r = Rack::Response.new(@body, @status, @headers)
410:       @cookies._n.each do |k, v|
411:         r.set_cookie(k, v)
412:       end
413:       r.to_a
414:     end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.