Skip to content
Newer
Older
100644 638 lines (610 sloc) 21.7 KB
d5f0d99 * lib/camping.rb: debugged qs_parse further, added multiple value su…
_why authored Jan 26, 2006
1 # == About camping.rb
2 #
3 # Camping comes with two versions of its source code. The code contained in
4 # lib/camping.rb is compressed, stripped of whitespace, using compact algorithms
5 # to keep it tight. The unspoken rule is that camping.rb should be flowed with
6 # no more than 80 characters per line and must not exceed four kilobytes.
7 #
8 # On the other hand, lib/camping-unabridged.rb contains the same code, laid out
9 # nicely with piles of documentation everywhere. This documentation is entirely
10 # generated from lib/camping-unabridged.rb using RDoc and our "flipbook" template
11 # found in the extras directory of any camping distribution.
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
12 #
13 # == Requirements
14 #
15 # Camping requires at least Ruby 1.8.2.
16 #
17 # Camping depends on the following libraries. If you install through RubyGems,
18 # these will be automatically installed for you.
19 #
20 # * ActiveRecord, used in your models.
21 # ActiveRecord is an object-to-relational database mapper with adapters
22 # for SQLite3, MySQL, PostgreSQL, SQL Server and more.
23 # * Markaby, used in your views to describe HTML in plain Ruby.
24 #
25 # Camping also works well with Mongrel, the swift Ruby web server.
26 # http://rubyforge.org/projects/mongrel Mongrel comes with examples
27 # in its <tt>examples/camping</tt> directory.
28 #
26cd515 @zimbatm Tempfile is not needed anymore
zimbatm authored Aug 28, 2008
29 %w[uri stringio rack].map { |l| require l }
6724a04 @zimbatm Removed Markaby as a direct dependency. As AR, it is loaded on demand.
zimbatm authored Oct 1, 2007
30
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
31 class Object #:nodoc:
32 def meta_def(m,&b) #:nodoc:
4b6a2c8 @zimbatm micro-optimizations and fix for ticket #96 : more strictness for Help…
zimbatm authored Oct 2, 2007
33 (class<<self;self end).send(:define_method,m,&b)
6724a04 @zimbatm Removed Markaby as a direct dependency. As AR, it is loaded on demand.
zimbatm authored Oct 1, 2007
34 end
35 end
abc7043 Initial import.
_why authored Jan 18, 2006
36
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
37 # == Camping
31e25ad README: top-level docs.
_why authored Jan 20, 2006
38 #
39 # The camping module contains three modules for separating your application:
40 #
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
41 # * Camping::Models for your database interaction classes, all derived from ActiveRecord::Base.
31e25ad README: top-level docs.
_why authored Jan 20, 2006
42 # * Camping::Controllers for storing controller classes, which map URLs to code.
43 # * Camping::Views for storing methods which generate HTML.
44 #
45 # Of use to you is also one module for storing helpful additional methods:
46 #
47 # * Camping::Helpers which can be used in controllers and views.
48 #
f062c5b * lib/camping-unabridged.rb: remove most of the postamble docs, exce…
_why authored Sep 1, 2006
49 # == The Camping Server
31e25ad README: top-level docs.
_why authored Jan 20, 2006
50 #
f062c5b * lib/camping-unabridged.rb: remove most of the postamble docs, exce…
_why authored Sep 1, 2006
51 # How do you run Camping apps? Oh, uh... The Camping Server!
31e25ad README: top-level docs.
_why authored Jan 20, 2006
52 #
f062c5b * lib/camping-unabridged.rb: remove most of the postamble docs, exce…
_why authored Sep 1, 2006
53 # The Camping Server is, firstly and thusly, a set of rules. At the very least, The Camping Server must:
31e25ad README: top-level docs.
_why authored Jan 20, 2006
54 #
f062c5b * lib/camping-unabridged.rb: remove most of the postamble docs, exce…
_why authored Sep 1, 2006
55 # * Load all Camping apps in a directory.
56 # * Load new apps that appear in that directory.
57 # * Mount those apps according to their filename. (e.g. blog.rb is mounted at /blog.)
58 # * Run each app's <tt>create</tt> method upon startup.
59 # * Reload the app if its modification time changes.
60 # * Reload the app if it requires any files under the same directory and one of their modification times changes.
61 # * Support the X-Sendfile header.
31e25ad README: top-level docs.
_why authored Jan 20, 2006
62 #
f062c5b * lib/camping-unabridged.rb: remove most of the postamble docs, exce…
_why authored Sep 1, 2006
63 # In fact, Camping comes with its own little The Camping Server.
64 #
65 # At a command prompt, run: <tt>camping examples/</tt> and the entire <tt>examples/</tt> directory will be served.
66 #
67 # Configurations also exist for Apache and Lighttpd. See http://code.whytheluckystiff.net/camping/wiki/TheCampingServer.
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
68 #
69 # == The <tt>create</tt> method
70 #
71 # Many postambles will check for your application's <tt>create</tt> method and will run it
72 # when the web server starts up. This is a good place to check for database tables and create
73 # those tables to save users of your application from needing to manually set them up.
74 #
75 # def Blog.create
76 # unless Blog::Models::Post.table_exists?
77 # ActiveRecord::Schema.define do
78 # create_table :blog_posts, :force => true do |t|
79 # t.column :user_id, :integer, :null => false
80 # t.column :title, :string, :limit => 255
81 # t.column :body, :text
82 # end
83 # end
84 # end
85 # end
86 #
87 # For more tips, see http://code.whytheluckystiff.net/camping/wiki/GiveUsTheCreateMethod.
abc7043 Initial import.
_why authored Jan 18, 2006
88 module Camping
616d685 lib/camping.rb: redirect now picks the route most likely.
_why authored Jan 20, 2006
89 C = self
fa83d7b @zimbatm -40 octets, we're now at 4040
zimbatm authored Sep 24, 2007
90 S = IO.read(__FILE__) rescue nil
d1c2c7f @judofyr Small fixes
judofyr authored May 21, 2008
91 P = "<h1>Cam\ping Problem!</h1><h2>%s</h2>"
7eabcbb @judofyr Earning more bytes
judofyr authored Nov 13, 2008
92 U = Rack::Utils
eb75914 @judofyr Less magic Camping::Apps
judofyr authored Aug 31, 2008
93 Apps = []
819f584 @zimbatm ActiveSupport dependency gone ! H is now simply inheriting from Hash.
zimbatm authored Sep 30, 2007
94 # An object-like Hash.
427f085 * lib/camping-unabridged.rb: switched from OpenStruct to HashWithInd…
_why authored Jan 25, 2006
95 # All Camping query string and cookie variables are loaded as this.
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
96 #
97 # To access the query string, for instance, use the <tt>@input</tt> variable.
98 #
c92fece @zimbatm Fixed ticket #132. Documentation issue.
zimbatm authored Sep 17, 2007
99 # module Blog::Controllers
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
100 # class Index < R '/'
101 # def get
102 # if page = @input.page.to_i > 0
103 # page -= 1
104 # end
105 # @posts = Post.find :all, :offset => page * 20, :limit => 20
106 # render :index
107 # end
108 # end
109 # end
110 #
111 # In the above example if you visit <tt>/?page=2</tt>, you'll get the second
112 # page of twenty posts. You can also use <tt>@input[:page]</tt> or <tt>@input['page']</tt>
113 # to get the value for the <tt>page</tt> query variable.
114 #
115 # Use the <tt>@cookies</tt> variable in the same fashion to access cookie variables.
819f584 @zimbatm ActiveSupport dependency gone ! H is now simply inheriting from Hash.
zimbatm authored Sep 30, 2007
116 class H < Hash
08f3318 * lib/camping.rb: allow markaby fragments to come out of controllers…
_why authored May 26, 2006
117 # Gets or sets keys in the hash.
118 #
119 # @cookies.my_favorite = :macadamian
120 # @cookies.my_favorite
121 # => :macadamian
122 #
a5c5561 * lib/camping.rb: trimmed about 100 bytes here and there. also, patc…
_why authored Jan 31, 2006
123 def method_missing(m,*a)
819f584 @zimbatm ActiveSupport dependency gone ! H is now simply inheriting from Hash.
zimbatm authored Sep 30, 2007
124 m.to_s=~/=$/?self[$`]=a[0]:a==[]?self[m.to_s]:super
66f83dd lib/camping.rb: trying to get the HashWithIndifferentAccess to work m…
_why authored Jan 26, 2006
125 end
4b6a2c8 @zimbatm micro-optimizations and fix for ticket #96 : more strictness for Help…
zimbatm authored Oct 2, 2007
126 undef id, type
427f085 * lib/camping-unabridged.rb: switched from OpenStruct to HashWithInd…
_why authored Jan 25, 2006
127 end
128
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
129 # Helpers contains methods available in your controllers and views. You may add
130 # methods of your own to this module, including many helper methods from Rails.
131 # This is analogous to Rails' <tt>ApplicationHelper</tt> module.
132 #
133 # == Using ActionPack Helpers
134 #
135 # If you'd like to include helpers from Rails' modules, you'll need to look up the
136 # helper module in the Rails documentation at http://api.rubyonrails.org/.
137 #
138 # For example, if you look up the <tt>ActionView::Helpers::FormHelper</tt> class,
139 # you'll find that it's loaded from the <tt>action_view/helpers/form_helper.rb</tt>
140 # file. You'll need to have the ActionPack gem installed for this to work.
141 #
142 # require 'action_view/helpers/form_helper.rb'
143 #
144 # # This example is unfinished.. soon..
145 #
616d685 lib/camping.rb: redirect now picks the route most likely.
_why authored Jan 20, 2006
146 module Helpers
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
147 # From inside your controllers and views, you will often need to figure out
148 # the route used to get to a certain controller +c+. Pass the controller class
149 # and any arguments into the R method, a string containing the route will be
150 # returned to you.
151 #
152 # Assuming you have a specific route in an edit controller:
153 #
154 # class Edit < R '/edit/(\d+)'
155 #
156 # A specific route to the Edit controller can be built with:
157 #
158 # R(Edit, 1)
159 #
160 # Which outputs: <tt>/edit/1</tt>.
161 #
162 # You may also pass in a model object and the ID of the object will be used.
163 #
164 # If a controller has many routes, the route will be selected if it is the
165 # first in the routing list to have the right number of arguments.
166 #
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
167 # == Using R in the View
168 #
169 # Keep in mind that this route doesn't include the root path.
170 # You will need to use <tt>/</tt> (the slash method above) in your controllers.
171 # Or, go ahead and use the Helpers#URL method to build a complete URL for a route.
172 #
173 # However, in your views, the :href, :src and :action attributes automatically
174 # pass through the slash method, so you are encouraged to use <tt>R</tt> or
175 # <tt>URL</tt> in your views.
176 #
177 # module Blog::Views
178 # def menu
179 # div.menu! do
180 # a 'Home', :href => URL()
181 # a 'Profile', :href => "/profile"
182 # a 'Logout', :href => R(Logout)
183 # a 'Google', :href => 'http://google.com'
184 # end
185 # end
186 # end
187 #
188 # Let's say the above example takes place inside an application mounted at
189 # <tt>http://localhost:3301/frodo</tt> and that a controller named <tt>Logout</tt>
190 # is assigned to route <tt>/logout</tt>. The HTML will come out as:
191 #
192 # <div id="menu">
e10a609 @judofyr Only create Rack::Response on demand
judofyr authored Jan 24, 2009
193 # <a href="http://localhost:3301/frodo/">Home</a>
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
194 # <a href="/frodo/profile">Profile</a>
195 # <a href="/frodo/logout">Logout</a>
196 # <a href="http://google.com">Google</a>
197 # </div>
198 #
a1fa3d7 * lib/camping.rb: new support for ordered controllers. uses the `inh…
_why authored Jul 11, 2006
199 def R(c,*g)
696e702 * lib/camping.rb: added query-string building to `R()`.
_why authored Jan 6, 2007
200 p,h=/\(.+?\)/,g.grep(Hash)
4b6a2c8 @zimbatm micro-optimizations and fix for ticket #96 : more strictness for Help…
zimbatm authored Oct 2, 2007
201 g-=h
202 raise "bad route" unless u = c.urls.find{|x|
203 break x if x.scan(p).size == g.size &&
204 /^#{x}\/?$/ =~ (x=g.inject(x){|x,a|
7eabcbb @judofyr Earning more bytes
judofyr authored Nov 13, 2008
205 x.sub p,U.escape((a[a.class.primary_key]rescue a))})
4b6a2c8 @zimbatm micro-optimizations and fix for ticket #96 : more strictness for Help…
zimbatm authored Oct 2, 2007
206 }
7eabcbb @judofyr Earning more bytes
judofyr authored Nov 13, 2008
207 h.any?? u+"?"+U.build_query(h[0]) : u
616d685 lib/camping.rb: redirect now picks the route most likely.
_why authored Jan 20, 2006
208 end
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
209
2989e76 * lib/camping-unabridged.rb: corrected the docs for URL(), which giv…
_why authored May 19, 2006
210 # Simply builds a complete path from a path +p+ within the app. If your application is
211 # mounted at <tt>/blog</tt>:
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
212 #
61e9b19 lib/camping.rb: added Camping.goes, bug in routing.
_why authored Jan 21, 2006
213 # self / "/view/1" #=> "/blog/view/1"
214 # self / "styles.css" #=> "styles.css"
663796c camping.gemspec: generate rdoc.
_why authored Jan 22, 2006
215 # self / R(Edit, 1) #=> "/blog/edit/1"
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
216 #
7dd6555 @judofyr Earning some bytes by using a regex, and earning some performance by …
judofyr authored Nov 13, 2008
217 def /(p); p[0]==?/?@root+p:p end
43fbe7c * lib/camping.rb: Helpers#URL returns a URI object. This way relativ…
_why authored Feb 13, 2006
218 # Builds a URL route to a controller or a path, returning a URI object.
219 # This way you'll get the hostname and the port number, a complete URL.
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
220 #
221 # You can use this to grab URLs for controllers using the R-style syntax.
222 # So, if your application is mounted at <tt>http://test.ing/blog/</tt>
223 # and you have a View controller which routes as <tt>R '/view/(\d+)'</tt>:
224 #
04eeaf4 @judofyr Helpers#URL will now prepend the scheme
judofyr authored May 21, 2008
225 # URL(View, @post.id) #=> #<URL:http://test.ing/blog/view/12>
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
226 #
227 # Or you can use the direct path:
228 #
04eeaf4 @judofyr Helpers#URL will now prepend the scheme
judofyr authored May 21, 2008
229 # self.URL #=> #<URL:http://test.ing/blog/>
230 # self.URL + "view/12" #=> #<URL:http://test.ing/blog/view/12>
231 # URL("/view/12") #=> #<URL:http://test.ing/blog/view/12>
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
232 #
43fbe7c * lib/camping.rb: Helpers#URL returns a URI object. This way relativ…
_why authored Feb 13, 2006
233 # It's okay to pass URL strings through this method as well:
234 #
04eeaf4 @judofyr Helpers#URL will now prepend the scheme
judofyr authored May 21, 2008
235 # URL("http://google.com") #=> #<URL:http://google.com>
43fbe7c * lib/camping.rb: Helpers#URL returns a URI object. This way relativ…
_why authored Feb 13, 2006
236 #
237 # Any string which doesn't begin with a slash will pass through
238 # unscathed.
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
239 def URL c='/',*a
240 c = R(c, *a) if c.respond_to? :urls
241 c = self/c
7dd6555 @judofyr Earning some bytes by using a regex, and earning some performance by …
judofyr authored Nov 13, 2008
242 c = @request.url[/.{8,}?(?=\/)/]+c if c[0]==?/
43fbe7c * lib/camping.rb: Helpers#URL returns a URI object. This way relativ…
_why authored Feb 13, 2006
243 URI(c)
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
244 end
616d685 lib/camping.rb: redirect now picks the route most likely.
_why authored Jan 20, 2006
245 end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
246
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
247 # Camping::Base is built into each controller by way of the generic routing
248 # class Camping::R. In some ways, this class is trying to do too much, but
249 # it saves code for all the glue to stay in one place.
250 #
251 # Forgivable, considering that it's only really a handful of methods and accessors.
252 #
253 # == Treating controller methods like Response objects
254 #
255 # Camping originally came with a barebones Response object, but it's often much more readable
256 # to just use your controller as the response.
257 #
258 # Go ahead and alter the status, cookies, headers and body instance variables as you
259 # see fit in order to customize the response.
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
260 #
261 # module Camping::Controllers
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
262 # class SoftLink
263 # def get
264 # redirect "/"
265 # end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
266 # end
267 # end
268 #
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
269 # Is equivalent to:
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
270 #
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
271 # module Camping::Controllers
272 # class SoftLink
273 # def get
274 # @status = 302
275 # @headers['Location'] = "/"
276 # end
277 # end
278 # end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
279 #
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
280 module Base
b09de27 @judofyr Removing @env - use @headers and @request instead
judofyr authored May 21, 2008
281 attr_accessor :input, :cookies, :headers, :body, :status, :root
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
282
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
283 # Display a view, calling it by its method name +m+. If a <tt>layout</tt>
284 # method is found in Camping::Views, it will be used to wrap the HTML.
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
285 #
286 # module Camping::Controllers
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
287 # class Show
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
288 # def get
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
289 # @posts = Post.find :all
290 # render :index
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
291 # end
292 # end
293 # end
294 #
e77edad @zimbatm Changed Base#method_missing to Base#mab
zimbatm authored May 25, 2008
295 # You can also return directly html by just passing a block
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
296 #
e77edad @zimbatm Changed Base#method_missing to Base#mab
zimbatm authored May 25, 2008
297 def render(v,*a,&b)
298 mab(/^_/!~v.to_s){send(v,*a,&b)}
299 end
300
301 # You can directly return HTML form your controller for quick debugging
302 # by calling this method and pass some Markaby to it.
303 #
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
304 # module Camping::Controllers
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
305 # class Info
e77edad @zimbatm Changed Base#method_missing to Base#mab
zimbatm authored May 25, 2008
306 # def get; mab{ code @headers.inspect } end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
307 # end
308 # end
309 #
e77edad @zimbatm Changed Base#method_missing to Base#mab
zimbatm authored May 25, 2008
310 # You can also pass true to use the :layout HTML wrapping method
311 #
312 def mab(l=nil,&b)
2a606f7 * lib/camping.rb: novaa saves us 54 pennybits.
_why authored Aug 17, 2006
313 m=Mab.new({},self)
e77edad @zimbatm Changed Base#method_missing to Base#mab
zimbatm authored May 25, 2008
314 s=m.capture(&b)
315 s=m.capture{layout{s}} if l && m.respond_to?(:layout)
5a49057 * lib/camping.rb: let's not cast Markaby to a string by default, tha…
_why authored Jul 31, 2006
316 s
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
317 end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
318
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
319 # A quick means of setting this controller's status, body and headers.
320 # Used internally by Camping, but... by all means...
321 #
322 # r(302, '', 'Location' => self / "/view/12")
323 #
324 # Is equivalent to:
325 #
326 # redirect "/view/12"
327 #
7b0c6f7 @judofyr Base#r supports the Rack-array too
judofyr authored May 21, 2008
328 # You can also switch the body and the header in order to support Rack:
329 #
330 # r(302, {'Location' => self / "/view/12"}, '')
e10a609 @judofyr Only create Rack::Response on demand
judofyr authored Jan 23, 2009
331 # r(another_app.call(@env))
7b0c6f7 @judofyr Base#r supports the Rack-array too
judofyr authored May 21, 2008
332 #
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
333 # See also: #r404, #r500 and #r501
d55d735 @judofyr First attempt to use Rack
judofyr authored May 20, 2008
334 def r(s, b, h = {})
5359649 @judofyr Earning some more bytes
judofyr authored Oct 29, 2008
335 b, h = h, b if Hash === b
d55d735 @judofyr First attempt to use Rack
judofyr authored May 20, 2008
336 @status = s
337 @headers.merge!(h)
338 @body = b
339 end
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
340
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
341 # Formulate a redirect response: a 302 status with <tt>Location</tt> header
342 # and a blank body. Uses Helpers#URL to build the location from a controller
343 # route or path.
344 #
345 # So, given a root of <tt>http://localhost:3301/articles</tt>:
346 #
98e4333 * lib: hiding useless things in the docs.
_why authored Jul 12, 2006
347 # redirect "view/12" # redirects to "//localhost:3301/articles/view/12"
348 # redirect View, 12 # redirects to "//localhost:3301/articles/view/12"
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
349 #
08f3318 * lib/camping.rb: allow markaby fragments to come out of controllers…
_why authored May 26, 2006
350 # <b>NOTE:</b> This method doesn't magically exit your methods and redirect.
351 # You'll need to <tt>return redirect(...)</tt> if this isn't the last statement
352 # in your code.
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
353 def redirect(*a)
d1c2c7f @judofyr Small fixes
judofyr authored May 21, 2008
354 r(302,'','Location'=>URL(*a).to_s)
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
355 end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
356
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
357 # Called when a controller was not found. It is mainly used internally, but it can
358 # also be useful for you, if you want to filter some parameters.
359 #
360 # module Camping
361 # def r404(p=env.PATH)
362 # @status = 404
363 # div do
364 # h1 'Camping Problem!'
365 # h2 "#{p} not found"
366 # end
367 # end
368 # end
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
369 #
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
370 # See: I
371 def r404(p=env.PATH)
372 r(404, P % "#{p} not found")
373 end
374
375 # If there is a parse error in Camping or in your application's source code, it will not be caught
376 # by Camping. The controller class +k+ and request method +m+ (GET, POST, etc.) where the error
377 # took place are passed in, along with the Exception +e+ which can be mined for useful info.
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
378 #
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
379 # You can overide it, but if you have an error in here, it will be uncaught !
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
380 #
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
381 # See: I
382 def r500(k,m,x)
383 r(500, P % "#{k}.#{m}" + "<h3>#{x.class} #{x.message}: <ul>#{x.backtrace.map{|b|"<li>#{b}</li>"}}</ul></h3>")
384 end
385
386 # Called if an undefined method is called on a Controller, along with the request method +m+ (GET, POST, etc.)
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
387 #
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
388 # See: I
389 def r501(m=@method)
390 r(501, P % "#{m.upcase} not implemented")
391 end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
392
864472e * lib/camping.rb: committing Base#to_a (discussed at http://redhande…
_why authored Jan 4, 2007
393 # Turn a controller into an array. This is designed to be used to pipe
394 # controllers into the <tt>r</tt> method. A great way to forward your
395 # requests!
396 #
397 # class Read < '/(\d+)'
398 # def get(id)
399 # Post.find(id)
400 # rescue
b09de27 @judofyr Removing @env - use @headers and @request instead
judofyr authored May 21, 2008
401 # r *Blog.get(:NotFound, @headers.REQUEST_URI)
864472e * lib/camping.rb: committing Base#to_a (discussed at http://redhande…
_why authored Jan 4, 2007
402 # end
403 # end
d55d735 @judofyr First attempt to use Rack
judofyr authored May 20, 2008
404 def to_a
e10a609 @judofyr Only create Rack::Response on demand
judofyr authored Jan 23, 2009
405 r = Rack::Response.new(@body, @status, @headers)
e03ff40 @judofyr First attempt to fix the cookie-system
judofyr authored Jun 6, 2008
406 @cookies.each do |k, v|
407 v = {:value => v, :path => self / "/"} if String===v
e10a609 @judofyr Only create Rack::Response on demand
judofyr authored Jan 23, 2009
408 r.set_cookie(k, v)
e03ff40 @judofyr First attempt to fix the cookie-system
judofyr authored Jun 6, 2008
409 end
e10a609 @judofyr Only create Rack::Response on demand
judofyr authored Jan 23, 2009
410 r.to_a
d55d735 @judofyr First attempt to use Rack
judofyr authored May 20, 2008
411 end
412
bcc6e65 @judofyr Fixed #113
judofyr authored Jun 7, 2008
413 def initialize(env) #:nodoc:
e10a609 @judofyr Only create Rack::Response on demand
judofyr authored Jan 23, 2009
414 @request = Rack::Request.new(@env = env)
d56614c * Rakefile: use `git rev-list` to estimate a revision number.
_why authored May 24, 2008
415 @root, @input, @cookies,
c434eb9 @judofyr Cleaning up #service and #to_a
judofyr authored Jun 7, 2008
416 @headers, @status =
80984c1 @judofyr Fixing Camping.method_missing and unescape incoming urls
judofyr authored Nov 16, 2008
417 (@env.SCRIPT_NAME||'').sub(/\/$/,''),
95d2262 @judofyr Synchronizing camping.rb and camping-unabridged.rb
judofyr authored May 24, 2008
418 H[@request.params], H[@request.cookies],
e10a609 @judofyr Only create Rack::Response on demand
judofyr authored Jan 23, 2009
419 {}, 200
74aa21c @judofyr New try: This should really fix #113
judofyr authored Jun 11, 2008
420
95d2262 @judofyr Synchronizing camping.rb and camping-unabridged.rb
judofyr authored May 24, 2008
421 @input.each do |k, v|
422 if k[-2..-1] == "[]"
423 @input[k[0..-3]] = @input.delete(k)
653585c * lib/camping.rb: regexp wasn't allowing hash syntax (?post[author]=…
_why authored May 24, 2008
424 elsif k =~ /(.*)\[([^\]]+)\]$/
425 (@input[$1] ||= H[])[$2] = @input.delete(k)
4f59949 @judofyr Cleaning up @input after Rack
judofyr authored May 20, 2008
426 end
427 end
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
428 end
abc7043 Initial import.
_why authored Jan 18, 2006
429
08f3318 * lib/camping.rb: allow markaby fragments to come out of controllers…
_why authored May 26, 2006
430 # All requests pass through this method before going to the controller. Some magic
431 # in Camping can be performed by overriding this method.
432 #
433 # See http://code.whytheluckystiff.net/camping/wiki/BeforeAndAfterOverrides for more
434 # on before and after overrides with Camping.
435 def service(*a)
c380870 @judofyr Using @env instead of @request whenever it's possible. Earning some b…
judofyr authored Aug 14, 2008
436 r = catch(:halt){send(@env.REQUEST_METHOD.downcase, *a)}
c434eb9 @judofyr Cleaning up #service and #to_a
judofyr authored Jun 7, 2008
437 @body ||= r
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
438 self
439 end
440 end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
441
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
442 # Controllers is a module for placing classes which handle URLs. This is done
443 # by defining a route to each class using the Controllers::R method.
444 #
445 # module Camping::Controllers
446 # class Edit < R '/edit/(\d+)'
447 # def get; end
448 # def post; end
449 # end
450 # end
451 #
452 # If no route is set, Camping will guess the route from the class name.
453 # The rule is very simple: the route becomes a slash followed by the lowercased
454 # class name. See Controllers::D for the complete rules of dispatch.
60c72e2 @zimbatm Small doc fixes. Especially X = module Controllers; self;end was brea…
zimbatm authored Jun 27, 2008
455 module Controllers
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
456 @r = []
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
457 class << self
74f4a3d @zimbatm More doc fixes. Camping::X::M doc restored
zimbatm authored Jun 27, 2008
458 # An array containing the various controllers available for dispatch.
98e4333 * lib: hiding useless things in the docs.
_why authored Jul 12, 2006
459 def r #:nodoc:
460 @r
461 end
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
462 # Add routes to a controller class by piling them into the R method.
463 #
464 # module Camping::Controllers
465 # class Edit < R '/edit/(\d+)', '/new'
466 # def get(id)
467 # if id # edit
468 # else # new
469 # end
470 # end
471 # end
472 # end
473 #
474 # You will need to use routes in either of these cases:
475 #
476 # * You want to assign multiple routes to a controller.
477 # * You want your controller to receive arguments.
478 #
479 # Most of the time the rules inferred by dispatch method Controllers::D will get you
480 # by just fine.
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
481 def R *u
482 r=@r
483 Class.new {
484 meta_def(:urls){u}
485 meta_def(:inherited){|x|r<<x}
486 }
487 end
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
488
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
489 # Dispatch routes to controller classes.
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
490 # For each class, routes are checked for a match based on their order in the routing list
491 # given to Controllers::R. If no routes were given, the dispatcher uses a slash followed
492 # by the name of the controller lowercased.
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
493 #
494 # Controllers are searched in this order:
495 #
496 # # Classes without routes, since they refer to a very specific URL.
497 # # Classes with routes are searched in order of their creation.
498 #
499 # So, define your catch-all controllers last.
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
500 def D(p, m)
74aa21c @judofyr New try: This should really fix #113
judofyr authored Jun 11, 2008
501 p = '/' if !p || !p[0]
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
502 r.map { |k|
503 k.urls.map { |x|
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
504 return (k.instance_method(m) rescue nil) ?
505 [k, m, *$~[1..-1]] : [I, 'r501', m] if p =~ /^#{x}\/?$/
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
506 }
507 }
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
508 [I, 'r404', p]
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
509 end
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
510
74f4a3d @zimbatm More doc fixes. Camping::X::M doc restored
zimbatm authored Jun 27, 2008
511 N = H.new { |_,x| x.downcase }.merge! "N" => '(\d+)', "X" => '(\w+)', "Index" => ''
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
512 # The route maker, this is called by Camping internally, you shouldn't need to call it.
513 #
514 # Still, it's worth know what this method does. Since Ruby doesn't keep track of class
515 # creation order, we're keeping an internal list of the controllers which inherit from R().
516 # This method goes through and adds all the remaining routes to the beginning of the list
517 # and ensures all the controllers have the right mixins.
518 #
519 # Anyway, if you are calling the URI dispatcher from outside of a Camping server, you'll
520 # definitely need to call this at least once to set things up.
521 def M
98e4333 * lib: hiding useless things in the docs.
_why authored Jul 12, 2006
522 def M #:nodoc:
523 end
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
524 constants.map { |c|
525 k=const_get(c)
596f092 @zimbatm Gained 7 octets :)
zimbatm authored Sep 23, 2007
526 k.send :include,C,Base,Helpers,Models
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
527 @r=[k]+r if r-[k]==r
3c19581 * lib/camping.rb: controller class names influence their urls. Contr…
_why authored Jun 24, 2008
528 k.meta_def(:urls){["/#{c.scan(/.[^A-Z]*/).map(&N.method(:[]))*'/'}"]}if !k.respond_to?:urls
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
529 }
530 end
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
531 end
532
74f4a3d @zimbatm More doc fixes. Camping::X::M doc restored
zimbatm authored Jun 27, 2008
533 # Internal controller with no route. Used by #D and C.call to show internal messages.
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
534 class I < R()
abc7043 Initial import.
_why authored Jan 18, 2006
535 end
536 end
60c72e2 @zimbatm Small doc fixes. Especially X = module Controllers; self;end was brea…
zimbatm authored Jun 27, 2008
537 X = Controllers
abc7043 Initial import.
_why authored Jan 18, 2006
538
539 class << self
61e9b19 lib/camping.rb: added Camping.goes, bug in routing.
_why authored Jan 21, 2006
540 # When you are running many applications, you may want to create independent
541 # modules for each Camping application. Namespaces for each. Camping::goes
542 # defines a toplevel constant with the whole MVC rack inside.
543 #
544 # require 'camping'
545 # Camping.goes :Blog
546 #
547 # module Blog::Controllers; ... end
548 # module Blog::Models; ... end
549 # module Blog::Views; ... end
550 #
551 def goes(m)
5359649 @judofyr Earning some more bytes
judofyr authored Oct 29, 2008
552 Apps << eval(S.gsub(/Camping/,m.to_s), TOPLEVEL_BINDING)
61e9b19 lib/camping.rb: added Camping.goes, bug in routing.
_why authored Jan 21, 2006
553 end
d55d735 @judofyr First attempt to use Rack
judofyr authored May 20, 2008
554
60c72e2 @zimbatm Small doc fixes. Especially X = module Controllers; self;end was brea…
zimbatm authored Jun 27, 2008
555 # Ruby web servers use this method to enter the Camping realm. The e
556 # argument is the environment variables hash as per the Rack specification.
557 # And array with [statuc, headers, body] is expected at the output.
95d2262 @judofyr Synchronizing camping.rb and camping-unabridged.rb
judofyr authored May 24, 2008
558 def call(e)
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
559 X.M
95d2262 @judofyr Synchronizing camping.rb and camping-unabridged.rb
judofyr authored May 24, 2008
560 e = H[e.to_hash]
80984c1 @judofyr Fixing Camping.method_missing and unescape incoming urls
judofyr authored Nov 16, 2008
561 e.PATH_INFO = U.unescape(e.PATH_INFO)
d55d735 @judofyr First attempt to use Rack
judofyr authored May 20, 2008
562 k,m,*a=X.D e.PATH_INFO,(e.REQUEST_METHOD||'get').downcase
563 e.REQUEST_METHOD = m
0bc3b72 @judofyr Catching errors too
judofyr authored May 21, 2008
564 k.new(e).service(*a).to_a
abc7043 Initial import.
_why authored Jan 18, 2006
565 end
1109984 * lib/camping.rb: experimenting with Camping.method_missing as an an…
_why authored Jul 6, 2006
566
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
567 # The Camping scriptable dispatcher. Any unhandled method call to the app module will
568 # be sent to a controller class, specified as an argument.
569 #
570 # Blog.get(:Index)
571 # #=> #<Blog::Controllers::Index ... >
572 #
573 # The controller object contains all the @cookies, @body, @headers, etc. formulated by
574 # the response.
575 #
576 # You can also feed environment variables and query variables as a hash, the final
577 # argument.
578 #
579 # Blog.post(:Login, :input => {'username' => 'admin', 'password' => 'camping'})
580 # #=> #<Blog::Controllers::Login @user=... >
581 #
819f584 @zimbatm ActiveSupport dependency gone ! H is now simply inheriting from Hash.
zimbatm authored Sep 30, 2007
582 # Blog.get(:Info, :env => {'HTTP_HOST' => 'wagon'})
b09de27 @judofyr Removing @env - use @headers and @request instead
judofyr authored May 21, 2008
583 # #=> #<Blog::Controllers::Info @headers={'HTTP_HOST'=>'wagon'} ...>
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
584 #
1109984 * lib/camping.rb: experimenting with Camping.method_missing as an an…
_why authored Jul 6, 2006
585 def method_missing(m, c, *a)
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
586 X.M
3065665 @judofyr Camping#method_missing should work now
judofyr authored May 21, 2008
587 h=Hash===a[-1]?H[a.pop]:{}
860afd8 @zimbatm Removed Camping::H#u (alias to merge!)
zimbatm authored May 25, 2008
588 e=H[h[:env]||{}].merge!({'rack.input'=>StringIO.new,'REQUEST_METHOD'=>m.to_s})
3065665 @judofyr Camping#method_missing should work now
judofyr authored May 21, 2008
589 k = X.const_get(c).new(H[e])
590 k.send("input=",h[:input]) if h[:input]
5093efa @zimbatm +2 octets for the sake of ruby -w strickness validity
zimbatm authored Sep 23, 2007
591 k.service(*a)
1109984 * lib/camping.rb: experimenting with Camping.method_missing as an an…
_why authored Jul 6, 2006
592 end
abc7043 Initial import.
_why authored Jan 18, 2006
593 end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
594
0777ceb @zimbatm Synched camping and unabridged by using `rake diff`. Used attr_reader…
zimbatm authored Sep 24, 2007
595 # Views is an empty module for storing methods which create HTML. The HTML is described
596 # using the Markaby language.
597 #
598 # == Using the layout method
599 #
600 # If your Views module has a <tt>layout</tt> method defined, it will be called with a block
601 # which will insert content from your view.
602 module Views; include X, Helpers end
603
d3c7efe lib/camping.rb: trying to speed-up Markaby by creating the Mab class …
_why authored Jan 20, 2006
604 # Models is an empty Ruby module for housing model classes derived
605 # from ActiveRecord::Base. As a shortcut, you may derive from Base
606 # which is an alias for ActiveRecord::Base.
607 #
608 # module Camping::Models
609 # class Post < Base; belongs_to :user end
610 # class User < Base; has_many :posts end
611 # end
612 #
613 # == Where Models are Used
614 #
615 # Models are used in your controller classes. However, if your model class
616 # name conflicts with a controller class name, you will need to refer to it
617 # using the Models module.
618 #
619 # module Camping::Controllers
620 # class Post < R '/post/(\d+)'
621 # def get(post_id)
622 # @post = Models::Post.find post_id
623 # render :index
624 # end
625 # end
626 # end
627 #
628 # Models cannot be referred to in Views at this time.
377bc91 * lib/camping/db.rb: support for tracking migrations and the schema_…
_why authored Jul 31, 2006
629 module Models
56d4c3a @judofyr Replacing autoload of camping/db with camping/ar
judofyr authored May 24, 2008
630 autoload :Base,'camping/ar'
377bc91 * lib/camping/db.rb: support for tracking migrations and the schema_…
_why authored Jul 31, 2006
631 def Y;self;end
fdbf7ed * lib/camping.rb: auto-prefix tables to prevent name clash between s…
_why authored Jan 25, 2006
632 end
0777ceb @zimbatm Synched camping and unabridged by using `rake diff`. Used attr_reader…
zimbatm authored Sep 24, 2007
633
6724a04 @zimbatm Removed Markaby as a direct dependency. As AR, it is loaded on demand.
zimbatm authored Oct 1, 2007
634 autoload :Mab, 'camping/mab'
5359649 @judofyr Earning some more bytes
judofyr authored Oct 29, 2008
635 C
abc7043 Initial import.
_why authored Jan 18, 2006
636 end
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
637
Something went wrong with that request. Please try again.