Module Camping::Base
In: lib/camping-unabridged.rb

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 accessable inside your controllers.

Methods

mab   r   r404   r500   r501   redirect   render   service   table_name_prefix   to_a  

Attributes

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

Public Class methods

The default prefix for Camping model classes is the topmost module name lowercase and followed with an underscore.

  Tepee::Models::Page.table_name_prefix
    #=> "tepee_pages"

[Source]

    # File lib/camping/ar.rb, line 66
66:     def Base.table_name_prefix
67:         "#{name[/\w+/]}_".downcase.sub(/^(#{A}|camping)_/i,'')
68:     end

Public Instance methods

You can directly return HTML form your controller for quick debugging by calling this method and pass 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

[Source]

     # File lib/camping-unabridged.rb, line 265
265:     def mab(l=nil,&b)
266:       m=Mab.new({},self)
267:       s=m.capture(&b)
268:       s=m.capture{layout{s}} if l && m.respond_to?(:layout)
269:       s
270:     end

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: r404, r500 and r501

[Source]

     # File lib/camping-unabridged.rb, line 283
283:     def r(s, b, h = {})
284:       b, h = h, b if Hash === b
285:       @status = s
286:       @headers.merge!(h)
287:       @body = b
288:     end

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

[Source]

     # File lib/camping-unabridged.rb, line 317
317:     def r404(p)
318:       P % "#{p} not found"
319:     end

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

[Source]

     # File lib/camping-unabridged.rb, line 336
336:     def r500(k,m,e)
337:       raise e
338:     end

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

[Source]

     # File lib/camping-unabridged.rb, line 342
342:     def r501(m)
343:       P % "#{m.upcase} not implemented"
344:     end

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

[Source]

     # File lib/camping-unabridged.rb, line 304
304:     def redirect(*a)
305:       r(302,'','Location'=>URL(*a).to_s)
306:     end

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

[Source]

     # File lib/camping-unabridged.rb, line 251
251:     def render(v,*a,&b)
252:       mab(/^_/!~v.to_s){send(v,*a,&b)}
253:     end

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

[Source]

     # File lib/camping-unabridged.rb, line 390
390:     def service(*a)
391:       r = catch(:halt){send(@method, *a)}
392:       @body ||= r 
393:       self
394:     end

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

[Source]

     # File lib/camping-unabridged.rb, line 357
357:     def to_a
358:       @env['rack.session'] = @state
359:       r = Rack::Response.new(@body, @status, @headers)
360:       @cookies.each do |k, v|
361:         next if @old_cookies[k] == v
362:         v = { :value => v, :path => self / "/" } if String === v
363:         r.set_cookie(k, v)
364:       end
365:       r.to_a
366:     end

[Validate]