Module: Camping

Defined in:
lib/camping-unabridged.rb,
lib/camping.rb

Overview

Camping

The camping module contains three modules for separating your application:

  • Camping::Models for storing classes derived from ActiveRecord::Base.

  • Camping::Controllers for storing controller classes, which map URLs to code.

  • Camping::Views for storing methods which generate HTML.

Of use to you is also one module for storing helpful additional methods:

  • Camping::Helpers which can be used in controllers and views.

The postamble

Most Camping applications contain the entire application in a single script. The script begins by requiring Camping, then fills each of the three modules described above with classes and methods. Finally, a postamble puts the wheels in motion.

if __FILE__ == $0
  Camping::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog3.db'
  Camping::Models::Base.logger = Logger.new('camping.log')
  Camping.run
end

In the postamble, your job is to setup Camping::Models::Base (see: ActiveRecord::Base) and call Camping::run in a request loop. The above postamble is for a standard CGI setup, where the web server manages the request loop and calls the script once for every request.

For other configurations, see code.whytheluckystiff.net/camping/wiki/PostAmbles

Defined Under Namespace

Modules: Controllers, Helpers, Models, Views Classes: Mab

Constant Summary collapse

C =
self
S =
File.read(__FILE__).gsub(/_{2}FILE_{2}/,__FILE__.dump)

Class Method Summary collapse

Class Method Details

Parses a string of cookies from the Cookie header.



379
# File 'lib/camping-unabridged.rb', line 379

def cookie_parse(s); c = qs_parse(s, ';,'); end

.escape(s) ⇒ Object

URL escapes a string.

Camping.escape("I'd go to the museum straightway!")  
  #=> "I%27d+go+to+the+museum+straightway%21"


361
362
363
# File 'lib/camping-unabridged.rb', line 361

def 
escape s;s.to_s.gsub(/([^ a-zA-Z0-9_.-]+)/n){'%'+$1.unpack('H2'*$1.size).join(
'%').upcase}.tr(' ','+') end

.goes(m) ⇒ Object

When you are running many applications, you may want to create independent modules for each Camping application. Namespaces for each. Camping::goes defines a toplevel constant with the whole MVC rack inside.

require 'camping'
Camping.goes :Blog

module Blog::Controllers; ... end
module Blog::Models;      ... end
module Blog::Views;       ... end


351
# File 'lib/camping-unabridged.rb', line 351

def goes m;eval(S.gsub(/Camping/,m.to_s),TOPLEVEL_BINDING)end

.kp(s) ⇒ Object



41
# File 'lib/camping.rb', line 41

def kp(s);c=qs_parse(s,';,') end

.qs_parse(qs, d = '&;') ⇒ Object

Parses a query string into an OpenStruct object.

input = Camping.qs_parse("name=Philarp+Tremain&hair=sandy+blonde")
input.name
  #=> "Philarp Tremaine"


375
376
377
# File 'lib/camping-unabridged.rb', line 375

def qs_parse(qs,d ='&;');(qs||''
).split(/[#{d}] */n).inject({}){|hsh, p|k,v=p.split('=',2).map{|v|unescape(v)}
hsh[k]=v unless v.blank?;hsh} end

.run(r = $stdin, w = $stdout) ⇒ Object

Fields a request through Camping. For traditional CGI applications, the method can be executed without arguments.

if __FILE__ == $0
  Camping::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog3.db'
  Camping::Models::Base.logger = Logger.new('camping.log')
  Camping.run
end

For FastCGI and Webrick-loaded applications, you will need to use a request loop, with run at the center, passing in the read r and write w streams. You will also need to mimick or replace ENV as part of your wrapper.

if __FILE__ == $0
  require 'fcgi'
    Camping::Models::Base.establish_connection :adapter => 'sqlite3', :database => 'blog3.db'
    Camping::Models::Base.logger = Logger.new('camping.log')
    FCGI.each do |req|
      ENV.replace req.env
      Camping.run req.in, req.out
      req.finish
    end
  end
end


406
407
408
409
# File 'lib/camping-unabridged.rb', line 406

def run(r=$stdin,w=$stdout);w<<begin;k,a=Controllers.D "/#{ENV['PATH_INFO']}".
gsub(%r!/+!,'/');m=ENV['REQUEST_METHOD']||"GET";k.class_eval{include C
include Controllers::Base;include Models};o=k.new;o.service(r,ENV,m,a);rescue\
=>e;Controllers::ServerError.new.service(r,ENV,"GET",[k,m,e]);end;end

.unescape(s) ⇒ Object

Unescapes a URL-encoded string.

Camping.unescape("I%27d+go+to+the+museum+straightway%21") 
  #=> "I'd go to the museum straightway!"


367
368
# File 'lib/camping-unabridged.rb', line 367

def unescape(s);s.tr('+', ' ').gsub(/((?:%[0-9a-f\
A-F]{2})+)/n){[$1.delete('%')].pack('H*')} end