Skip to content
Newer
Older
100644 739 lines (702 sloc) 23.5 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
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
9 # nicely with piles of documentation everywhere. This documentation is entirely
d5f0d99 * lib/camping.rb: debugged qs_parse further, added multiple value su…
_why authored Jan 26, 2006
10 # generated from lib/camping-unabridged.rb using RDoc and our "flipbook" template
11 # found in the extras directory of any camping distribution.
6e6fe56 @busbey camping/camping #8 is implemented now. so short.
busbey authored Mar 27, 2010
12 require "uri"
13 require "rack"
6724a04 @zimbatm Removed Markaby as a direct dependency. As AR, it is loaded on demand.
zimbatm authored Oct 1, 2007
14
09ee34f @judofyr camping-unabridged.rb: Fake that it has loaded camping.rb
judofyr authored Apr 25, 2010
15 $LOADED_FEATURES << "camping.rb"
16
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
17 class Object #:nodoc:
18 def meta_def(m,&b) #:nodoc:
4b6a2c8 @zimbatm micro-optimizations and fix for ticket #96 : more strictness for Help…
zimbatm authored Oct 2, 2007
19 (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
20 end
21 end
abc7043 Initial import.
_why authored Jan 18, 2006
22
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
23 # If you're new to Camping, you should probably start by reading the first
24 # chapters of {The Camping Book}[file:book/01_introduction.html#toc].
f15d38d @judofyr Cleaned up and wrote some documentation
judofyr authored Feb 28, 2009
25 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
26 # Okay. So, the important thing to remember is that <tt>Camping.goes :Nuts</tt>
27 # copies the Camping module into Nuts. This means that you should never use
28 # any of these methods/classes on the Camping module, but rather on your own
29 # app. Here's a short explanation on how Camping is organized:
30 #
31 # * Camping::Controllers is where your controllers live.
32 # * Camping::Models is where your models live.
33 # * Camping::Views is where your views live.
34 # * Camping::Base is a module which is included in all your controllers.
35 # * Camping::Helpers is a module with useful helpers, both for the controllers
36 # and the views. You should fill this up with your own helpers.
f15d38d @judofyr Cleaned up and wrote some documentation
judofyr authored Feb 28, 2009
37 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
38 # Camping also ships with:
f15d38d @judofyr Cleaned up and wrote some documentation
judofyr authored Feb 28, 2009
39 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
40 # * Camping::Session adds states to your app.
41 # * Camping::Server starts up your app in development.
42 # * Camping::Reloader automatically reloads your apps when a file has changed.
f15d38d @judofyr Cleaned up and wrote some documentation
judofyr authored Feb 28, 2009
43 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
44 # More importantly, Camping also installs The Camping Server,
45 # please see Camping::Server.
abc7043 Initial import.
_why authored Jan 18, 2006
46 module Camping
616d685 lib/camping.rb: redirect now picks the route most likely.
_why authored Jan 20, 2006
47 C = self
fa83d7b @zimbatm -40 octets, we're now at 4040
zimbatm authored Sep 24, 2007
48 S = IO.read(__FILE__) rescue nil
d1c2c7f @judofyr Small fixes
judofyr authored May 21, 2008
49 P = "<h1>Cam\ping Problem!</h1><h2>%s</h2>"
7eabcbb @judofyr Earning more bytes
judofyr authored Nov 13, 2008
50 U = Rack::Utils
e3f70c5 @judofyr Introducing Camping.options
judofyr authored Apr 24, 2010
51 O = {}
eb75914 @judofyr Less magic Camping::Apps
judofyr authored Aug 31, 2008
52 Apps = []
819f584 @zimbatm ActiveSupport dependency gone ! H is now simply inheriting from Hash.
zimbatm authored Sep 30, 2007
53 # An object-like Hash.
427f085 * lib/camping-unabridged.rb: switched from OpenStruct to HashWithInd…
_why authored Jan 25, 2006
54 # 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
55 #
56 # To access the query string, for instance, use the <tt>@input</tt> variable.
57 #
c92fece @zimbatm Fixed ticket #132. Documentation issue.
zimbatm authored Sep 17, 2007
58 # module Blog::Controllers
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
59 # class Index < R '/'
60 # def get
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
61 # if (page = @input.page.to_i) > 0
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
62 # page -= 1
63 # end
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
64 # @posts = Post.all, :offset => page * 20, :limit => 20
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
65 # render :index
66 # end
67 # end
68 # end
69 #
70 # In the above example if you visit <tt>/?page=2</tt>, you'll get the second
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
71 # page of twenty posts. You can also use <tt>@input['page']</tt> to get the
72 # value for the <tt>page</tt> query variable.
819f584 @zimbatm ActiveSupport dependency gone ! H is now simply inheriting from Hash.
zimbatm authored Sep 30, 2007
73 class H < Hash
08f3318 * lib/camping.rb: allow markaby fragments to come out of controllers…
_why authored May 26, 2006
74 # Gets or sets keys in the hash.
75 #
76 # @cookies.my_favorite = :macadamian
77 # @cookies.my_favorite
78 # => :macadamian
79 #
a5c5561 * lib/camping.rb: trimmed about 100 bytes here and there. also, patc…
_why authored Jan 31, 2006
80 def method_missing(m,*a)
819f584 @zimbatm ActiveSupport dependency gone ! H is now simply inheriting from Hash.
zimbatm authored Sep 30, 2007
81 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
82 end
381b9cb @judofyr Attempt at 1.9 support (thanks babalela!)
judofyr authored Feb 21, 2010
83 undef id, type if ?? == 63
427f085 * lib/camping-unabridged.rb: switched from OpenStruct to HashWithInd…
_why authored Jan 25, 2006
84 end
84604bd @judofyr Refactored some documentation; added lots of TODOs. It's not written …
judofyr authored Feb 15, 2009
85
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
86 # Helpers contains methods available in your controllers and views. You may
87 # add methods of your own to this module, including many helper methods from
88 # Rails. This is analogous to Rails' <tt>ApplicationHelper</tt> module.
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
89 #
90 # == Using ActionPack Helpers
91 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
92 # If you'd like to include helpers from Rails' modules, you'll need to look
93 # up the helper module in the Rails documentation at http://api.rubyonrails.org/.
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
94 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
95 # For example, if you look up the <tt>ActionView::Helpers::FormTagHelper</tt>
96 # class, you'll find that it's loaded from the <tt>action_view/helpers/form_tag_helper.rb</tt>
97 # file. You'll need to have the ActionPack gem installed for this to work.
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
98 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
99 # Often the helpers depends on other helpers, so you would have to look up
100 # the dependencies too. <tt>FormTagHelper</tt> for instance required the
101 # <tt>content_tag</tt> provided by <tt>TagHelper</tt>.
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
102 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
103 # require 'action_view/helpers/form_tag_helper'
104 #
105 # module Nuts::Helpers
106 # include ActionView::Helpers::TagHelper
107 # include ActionView::Helpers::FormTagHelper
108 # end
cca5586 lib/camping-unabridged.rb: further documenting of the dependencies in…
_why authored Feb 1, 2006
109 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
110 # == Return a response immediately
111 # If you need to return a response inside a helper, you can use <tt>throw :halt</tt>.
112 #
113 # module Nuts::Helpers
114 # def requires_login!
115 # unless @state.user_id
116 # redirect Login
117 # throw :halt
118 # end
119 # end
120 # end
121 #
122 # module Nuts::Controllers
123 # class Admin
124 # def get
125 # requires_login!
126 # "Never gets here unless you're logged in"
127 # end
128 # end
129 # end
616d685 lib/camping.rb: redirect now picks the route most likely.
_why authored Jan 20, 2006
130 module Helpers
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
131 # From inside your controllers and views, you will often need to figure out
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
132 # the route used to get to a certain controller +c+. Pass the controller
133 # class and any arguments into the R method, a string containing the route
134 # will be returned to you.
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
135 #
136 # Assuming you have a specific route in an edit controller:
137 #
138 # class Edit < R '/edit/(\d+)'
139 #
140 # A specific route to the Edit controller can be built with:
141 #
142 # R(Edit, 1)
143 #
144 # Which outputs: <tt>/edit/1</tt>.
145 #
146 # If a controller has many routes, the route will be selected if it is the
147 # first in the routing list to have the right number of arguments.
148 #
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
149 # == Using R in the View
150 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
151 # Keep in mind that this route doesn't include the root path. You will
152 # need to use <tt>/</tt> (the slash method above) in your controllers.
153 # Or, go ahead and use the Helpers#URL method to build a complete URL for
154 # a route.
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
155 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
156 # However, in your views, the :href, :src and :action attributes
157 # automatically pass through the slash method, so you are encouraged to
158 # use <tt>R</tt> or <tt>URL</tt> in your views.
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
159 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
160 # module Nuts::Views
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
161 # def menu
162 # div.menu! do
163 # a 'Home', :href => URL()
164 # a 'Profile', :href => "/profile"
165 # a 'Logout', :href => R(Logout)
166 # a 'Google', :href => 'http://google.com'
167 # end
168 # end
169 # end
170 #
171 # Let's say the above example takes place inside an application mounted at
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
172 # <tt>http://localhost:3301/frodo</tt> and that a controller named
173 # <tt>Logout</tt> is assigned to route <tt>/logout</tt>.
174 # The HTML will come out as:
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
175 #
176 # <div id="menu">
e10a609 @judofyr Only create Rack::Response on demand
judofyr authored Jan 24, 2009
177 # <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
178 # <a href="/frodo/profile">Profile</a>
179 # <a href="/frodo/logout">Logout</a>
180 # <a href="http://google.com">Google</a>
181 # </div>
182 #
a1fa3d7 * lib/camping.rb: new support for ordered controllers. uses the `inh…
_why authored Jul 11, 2006
183 def R(c,*g)
696e702 * lib/camping.rb: added query-string building to `R()`.
_why authored Jan 6, 2007
184 p,h=/\(.+?\)/,g.grep(Hash)
4b6a2c8 @zimbatm micro-optimizations and fix for ticket #96 : more strictness for Help…
zimbatm authored Oct 2, 2007
185 g-=h
186 raise "bad route" unless u = c.urls.find{|x|
187 break x if x.scan(p).size == g.size &&
188 /^#{x}\/?$/ =~ (x=g.inject(x){|x,a|
7eabcbb @judofyr Earning more bytes
judofyr authored Nov 13, 2008
189 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
190 }
7eabcbb @judofyr Earning more bytes
judofyr authored Nov 13, 2008
191 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
192 end
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
193
f15d38d @judofyr Cleaned up and wrote some documentation
judofyr authored Feb 28, 2009
194 # Simply builds a complete path from a path +p+ within the app. If your
195 # application is mounted at <tt>/blog</tt>:
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
196 #
61e9b19 lib/camping.rb: added Camping.goes, bug in routing.
_why authored Jan 21, 2006
197 # self / "/view/1" #=> "/blog/view/1"
198 # self / "styles.css" #=> "styles.css"
663796c camping.gemspec: generate rdoc.
_why authored Jan 22, 2006
199 # self / R(Edit, 1) #=> "/blog/edit/1"
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
200 #
7dd6555 @judofyr Earning some bytes by using a regex, and earning some performance by …
judofyr authored Nov 13, 2008
201 def /(p); p[0]==?/?@root+p:p end
f15d38d @judofyr Cleaned up and wrote some documentation
judofyr authored Feb 28, 2009
202
43fbe7c * lib/camping.rb: Helpers#URL returns a URI object. This way relativ…
_why authored Feb 13, 2006
203 # Builds a URL route to a controller or a path, returning a URI object.
204 # 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
205 #
206 # You can use this to grab URLs for controllers using the R-style syntax.
207 # So, if your application is mounted at <tt>http://test.ing/blog/</tt>
208 # and you have a View controller which routes as <tt>R '/view/(\d+)'</tt>:
209 #
04eeaf4 @judofyr Helpers#URL will now prepend the scheme
judofyr authored May 21, 2008
210 # 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
211 #
212 # Or you can use the direct path:
213 #
04eeaf4 @judofyr Helpers#URL will now prepend the scheme
judofyr authored May 21, 2008
214 # self.URL #=> #<URL:http://test.ing/blog/>
215 # self.URL + "view/12" #=> #<URL:http://test.ing/blog/view/12>
216 # 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
217 #
43fbe7c * lib/camping.rb: Helpers#URL returns a URI object. This way relativ…
_why authored Feb 13, 2006
218 # It's okay to pass URL strings through this method as well:
219 #
04eeaf4 @judofyr Helpers#URL will now prepend the scheme
judofyr authored May 21, 2008
220 # 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
221 #
222 # Any string which doesn't begin with a slash will pass through
223 # unscathed.
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
224 def URL c='/',*a
225 c = R(c, *a) if c.respond_to? :urls
226 c = self/c
7dd6555 @judofyr Earning some bytes by using a regex, and earning some performance by …
judofyr authored Nov 13, 2008
227 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
228 URI(c)
9a2308d * lib/camping.rb: added Helpers.URL, which builds a complete URL to …
_why authored Feb 12, 2006
229 end
616d685 lib/camping.rb: redirect now picks the route most likely.
_why authored Jan 20, 2006
230 end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
231
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
232 # Camping::Base is built into each controller by way of the generic routing
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
233 # class Camping::R. In some ways, this class is trying to do too much, but
234 # it saves code for all the glue to stay in one place. Forgivable,
235 # considering that it's only really a handful of methods and accessors.
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
236 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
237 # Everything in this module is accessable inside your controllers.
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
238 module Base
7785812 @judofyr Export more ivars in Base
judofyr authored Nov 30, 2009
239 attr_accessor :env, :request, :root, :input, :cookies, :state,
240 :status, :headers, :body
8abffe8 @judofyr Documentate Camping.options
judofyr authored Apr 25, 2010
241
760c4b7 @judofyr Add Tilt support (closes #18)
judofyr authored Apr 25, 2010
242 T = {}
243 L = :layout
244
245 # Finds a template, returning either:
246 #
247 # false # => Could not find template
248 # true # => Found template in Views
249 # instance of Tilt # => Found template in a file
250 def lookup(n)
251 T.fetch(n.to_sym) do |k|
252 T[k] = Views.method_defined?(k) ||
253 (f = Dir[[O[:views] || "views", "#{n}.*"]*'/'][0]) &&
254 Template.new(f, O[f[/\.(\w+)$/, 1].to_sym] || {})
255 end
256 end
257
f15d38d @judofyr Cleaned up and wrote some documentation
judofyr authored Feb 28, 2009
258 # Display a view, calling it by its method name +v+. If a <tt>layout</tt>
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
259 # method is found in Camping::Views, it will be used to wrap the HTML.
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
260 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
261 # module Nuts::Controllers
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
262 # class Show
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
263 # def get
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
264 # @posts = Post.find :all
265 # render :index
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
266 # end
267 # end
268 # end
269 #
760c4b7 @judofyr Add Tilt support (closes #18)
judofyr authored Apr 25, 2010
270 def render(v, o={}, &b)
271 if t = lookup(v)
272 s = (t == true) ? mab{ send(v, &b) } : t.render(self, o[:locals] || {}, &b)
273 s = render(L, o.merge(L => !?!)) { s } if o[L] != !?? && lookup(L)
274 s
275 else
276 raise "Can't find template #{v}"
277 end
e77edad @zimbatm Changed Base#method_missing to Base#mab
zimbatm authored May 25, 2008
278 end
279
280 # You can directly return HTML form your controller for quick debugging
281 # by calling this method and pass some Markaby to it.
282 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
283 # module Nuts::Controllers
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
284 # class Info
e77edad @zimbatm Changed Base#method_missing to Base#mab
zimbatm authored May 25, 2008
285 # def get; mab{ code @headers.inspect } end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
286 # end
287 # end
288 #
e77edad @zimbatm Changed Base#method_missing to Base#mab
zimbatm authored May 25, 2008
289 # You can also pass true to use the :layout HTML wrapping method
760c4b7 @judofyr Add Tilt support (closes #18)
judofyr authored Apr 25, 2010
290 def mab(&b)
291 (@mab ||= Mab.new({},self)).capture(&b)
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
292 end
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
293
294 # A quick means of setting this controller's status, body and headers
295 # based on a Rack response:
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
296 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
297 # r(302, 'Location' => self / "/view/12", '')
298 # r(*another_app.call(@env))
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
299 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
300 # You can also switch the body and the header if you want:
7b0c6f7 @judofyr Base#r supports the Rack-array too
judofyr authored May 21, 2008
301 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
302 # r(404, "Could not find page")
7b0c6f7 @judofyr Base#r supports the Rack-array too
judofyr authored May 21, 2008
303 #
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
304 # See also: #r404, #r500 and #r501
d55d735 @judofyr First attempt to use Rack
judofyr authored May 20, 2008
305 def r(s, b, h = {})
5359649 @judofyr Earning some more bytes
judofyr authored Oct 29, 2008
306 b, h = h, b if Hash === b
d55d735 @judofyr First attempt to use Rack
judofyr authored May 20, 2008
307 @status = s
308 @headers.merge!(h)
309 @body = b
310 end
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
311
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
312 # Formulate a redirect response: a 302 status with <tt>Location</tt> header
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
313 # and a blank body. Uses Helpers#URL to build the location from a
314 # controller route or path.
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
315 #
316 # So, given a root of <tt>http://localhost:3301/articles</tt>:
317 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
318 # redirect "view/12" # redirects to "//localhost:3301/articles/view/12"
319 # 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
320 #
08f3318 * lib/camping.rb: allow markaby fragments to come out of controllers…
_why authored May 26, 2006
321 # <b>NOTE:</b> This method doesn't magically exit your methods and redirect.
322 # You'll need to <tt>return redirect(...)</tt> if this isn't the last statement
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
323 # in your code, or <tt>throw :halt</tt> if it's in a helper.
f15d38d @judofyr Cleaned up and wrote some documentation
judofyr authored Feb 28, 2009
324 #
325 # See: Controllers
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
326 def redirect(*a)
d1c2c7f @judofyr Small fixes
judofyr authored May 21, 2008
327 r(302,'','Location'=>URL(*a).to_s)
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
328 end
c3e919e lib/camping.rb: docs!
_why authored Jan 20, 2006
329
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
330 # Called when a controller was not found. You can override this if you
331 # want to customize the error page:
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
332 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
333 # module Nuts
334 # def r404(path)
335 # @path = path
336 # render :not_found
337 # end
338 # end
64bd85c @judofyr Refactoring the exception handling
judofyr authored Jan 24, 2009
339 def r404(p)
340 P % "#{p} not found"
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
341 end
342
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
343 # Called when an exception is raised. However, if there is a parse error
344 # in Camping or in your application's source code, it will not be caught.
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
345 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
346 # +k+ is the controller class, +m+ is the request method (GET, POST, etc.)
347 # and +e+ is the Exception which can be mined for useful info.
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
348 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
349 # Be default this simply re-raises the error so a Rack middleware can
350 # handle it, but you are free to override it here:
351 #
352 # module Nuts
353 # def r500(klass, method, exception)
354 # send_email_alert(klass, method, exception)
355 # render :server_error
356 # end
357 # end
64bd85c @judofyr Refactoring the exception handling
judofyr authored Jan 23, 2009
358 def r500(k,m,e)
359 raise e
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
360 end
361
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
362 # Called if an undefined method is called on a controller, along with the
363 # request method +m+ (GET, POST, etc.)
64bd85c @judofyr Refactoring the exception handling
judofyr authored Jan 23, 2009
364 def r501(m)
365 P % "#{m.upcase} not implemented"
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
366 end
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
367
368 # Turn a controller into a Rack response. This is designed to be used to
369 # pipe controllers into the <tt>r</tt> method. A great way to forward your
864472e * lib/camping.rb: committing Base#to_a (discussed at http://redhande…
_why authored Jan 4, 2007
370 # requests!
371 #
372 # class Read < '/(\d+)'
373 # def get(id)
374 # Post.find(id)
375 # rescue
b09de27 @judofyr Removing @env - use @headers and @request instead
judofyr authored May 21, 2008
376 # r *Blog.get(:NotFound, @headers.REQUEST_URI)
864472e * lib/camping.rb: committing Base#to_a (discussed at http://redhande…
_why authored Jan 4, 2007
377 # end
378 # end
d55d735 @judofyr First attempt to use Rack
judofyr authored May 20, 2008
379 def to_a
07532a1 @judofyr Store the session as a Hash, not App::H
judofyr authored Apr 26, 2010
380 @env['rack.session'] = Hash[@state]
e10a609 @judofyr Only create Rack::Response on demand
judofyr authored Jan 23, 2009
381 r = Rack::Response.new(@body, @status, @headers)
e03ff40 @judofyr First attempt to fix the cookie-system
judofyr authored Jun 6, 2008
382 @cookies.each do |k, v|
99cfee3 @judofyr Cookies are now only set when changed (closes #1)
judofyr authored Nov 2, 2009
383 next if @old_cookies[k] == v
384 v = { :value => v, :path => self / "/" } if String === v
e10a609 @judofyr Only create Rack::Response on demand
judofyr authored Jan 23, 2009
385 r.set_cookie(k, v)
e03ff40 @judofyr First attempt to fix the cookie-system
judofyr authored Jun 6, 2008
386 end
e10a609 @judofyr Only create Rack::Response on demand
judofyr authored Jan 23, 2009
387 r.to_a
d55d735 @judofyr First attempt to use Rack
judofyr authored May 20, 2008
388 end
389
64bd85c @judofyr Refactoring the exception handling
judofyr authored Jan 23, 2009
390 def initialize(env, m) #:nodoc:
391 r = @request = Rack::Request.new(@env = env)
a7cff36 @judofyr Simplify things Rack gives us for free
judofyr authored Jun 11, 2009
392 @root, @input, @cookies, @state,
64bd85c @judofyr Refactoring the exception handling
judofyr authored Jan 23, 2009
393 @headers, @status, @method =
a7cff36 @judofyr Simplify things Rack gives us for free
judofyr authored Jun 11, 2009
394 r.script_name.sub(/\/$/,''), n(r.params),
99cfee3 @judofyr Cookies are now only set when changed (closes #1)
judofyr authored Nov 2, 2009
395 H[@old_cookies = r.cookies], H[r.session],
64bd85c @judofyr Refactoring the exception handling
judofyr authored Jan 23, 2009
396 {}, m =~ /r(\d+)/ ? $1.to_i : 200, m
a7cff36 @judofyr Simplify things Rack gives us for free
judofyr authored Jun 11, 2009
397 end
398
399 def n(h) # :nodoc:
400 if Hash === h
401 h.inject(H[]) do |m, (k, v)|
402 m[k] = n(v)
403 m
404 end
405 else
406 h
4f59949 @judofyr Cleaning up @input after Rack
judofyr authored May 20, 2008
407 end
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
408 end
abc7043 Initial import.
_why authored Jan 18, 2006
409
f15d38d @judofyr Cleaned up and wrote some documentation
judofyr authored Feb 28, 2009
410 # All requests pass through this method before going to the controller.
411 # Some magic in Camping can be performed by overriding this method.
08f3318 * lib/camping.rb: allow markaby fragments to come out of controllers…
_why authored May 26, 2006
412 def service(*a)
64bd85c @judofyr Refactoring the exception handling
judofyr authored Jan 23, 2009
413 r = catch(:halt){send(@method, *a)}
c434eb9 @judofyr Cleaning up #service and #to_a
judofyr authored Jun 7, 2008
414 @body ||= r
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
415 self
416 end
417 end
84604bd @judofyr Refactored some documentation; added lots of TODOs. It's not written …
judofyr authored Feb 14, 2009
418
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
419
420 # Controllers receive the requests and sends a response back to the client.
421 # A controller is simply a class which must implement the HTTP methods it
422 # wants to accept:
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
423 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
424 # module Nuts::Controllers
425 # class Index
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
426 # def get
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
427 # "Hello World"
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
428 # end
429 # end
430 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
431 # class Posts
432 # def post
433 # Post.create(@input)
434 # redirect Index
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
435 # end
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
436 # end
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
437 # end
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
438 #
439 # == Defining a controller
440 #
441 # There are two ways to define controllers: Just defining a class and let
442 # Camping figure out the route, or add the route explicitly using R.
443 #
444 # If you don't use R, Camping will first split the controller name up by
445 # words (HelloWorld => Hello and World). Then it would do the following:
446 #
447 # * Replace Index with /
448 # * Replace X with ([^/]+)
449 # * Replace N with (\\\d+)
450 # * Everything else turns into lowercase
451 # * Join the words with slashes
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
452 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
453 #--
454 # NB! N will actually be replaced with (\d+), but it needs to be escaped
455 # here in order to work correctly with RDoc.
456 #++
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
457 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
458 # Here's a few examples:
459 #
460 # Index # => /
461 # PostN # => /post/(\d+)
462 # PageX # => /page/([^/]+)
463 # Pages # => /pages
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
464 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
465 # == The request
466 #
467 # You have these variables which describes the request:
468 #
469 # * @env contains the environment as defined in http://rack.rubyforge.org/doc/SPEC.html
470 # * @request is Rack::Request.new(@env)
471 # * @root is the path where the app is mounted
472 # * @cookies is a hash with the cookies sent by the client
473 # * @state is a hash with the sessions (see Camping::Session)
474 # * @method is the HTTP method in lowercase
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
475 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
476 # == The response
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
477 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
478 # You can change these variables to your needs:
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
479 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
480 # * @status is the HTTP status (defaults to 200)
481 # * @headers is a hash with the headers
482 # * @body is the body (a string or something which responds to #each)
483 # * Any changes in @cookies and @state will also be sent to the client
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
484 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
485 # If you haven't set @body, it will use the return value of the method:
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
486 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
487 # module Nuts::Controllers
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
488 # class Index
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
489 # def get
490 # "This is the body"
491 # end
9fc7fd8 @judofyr Started documentating Camping::Controllers
judofyr authored Mar 5, 2009
492 # end
493 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
494 # class Posts
495 # def get
496 # @body = "Hello World!"
497 # "This is ignored"
498 # end
a948a00 * lib/camping.rb: moved Controllers::Base to Base and Controllers::R…
_why authored Feb 21, 2006
499 # end
500 # end
60c72e2 @zimbatm Small doc fixes. Especially X = module Controllers; self;end was brea…
zimbatm authored Jun 27, 2008
501 module Controllers
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
502 @r = []
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
503 class << self
74f4a3d @zimbatm More doc fixes. Camping::X::M doc restored
zimbatm authored Jun 27, 2008
504 # An array containing the various controllers available for dispatch.
98e4333 * lib: hiding useless things in the docs.
_why authored Jul 12, 2006
505 def r #:nodoc:
506 @r
507 end
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
508
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
509 # Add routes to a controller class by piling them into the R method.
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
510 #
511 # The route is a regexp which will match the request path. Anything
512 # enclosed in parenthesis will be sent to the method as arguments.
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
513 #
514 # module Camping::Controllers
515 # class Edit < R '/edit/(\d+)', '/new'
516 # def get(id)
517 # if id # edit
518 # else # new
519 # end
520 # end
521 # end
522 # end
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
523 def R *u
524 r=@r
525 Class.new {
526 meta_def(:urls){u}
527 meta_def(:inherited){|x|r<<x}
528 }
529 end
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
530
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
531 # Dispatch routes to controller classes.
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
532 # For each class, routes are checked for a match based on their order in the routing list
533 # given to Controllers::R. If no routes were given, the dispatcher uses a slash followed
534 # by the name of the controller lowercased.
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
535 #
536 # Controllers are searched in this order:
537 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
538 # * Classes without routes, since they refer to a very specific URL.
539 # * Classes with routes are searched in order of their creation.
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
540 #
541 # So, define your catch-all controllers last.
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
542 def D(p, m)
74aa21c @judofyr New try: This should really fix #113
judofyr authored Jun 11, 2008
543 p = '/' if !p || !p[0]
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
544 r.map { |k|
545 k.urls.map { |x|
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
546 return (k.instance_method(m) rescue nil) ?
547 [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
548 }
549 }
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
550 [I, 'r404', p]
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
551 end
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
552
b96ee93 @judofyr Some bytesaving and make class PostX match /post/([^/])
judofyr authored Jan 24, 2009
553 N = H.new { |_,x| x.downcase }.merge! "N" => '(\d+)', "X" => '([^/]+)', "Index" => ''
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
554 # The route maker, this is called by Camping internally, you shouldn't
555 # need to call it.
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
556 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
557 # Still, it's worth know what this method does. Since Ruby doesn't keep
558 # track of class creation order, we're keeping an internal list of the
559 # controllers which inherit from R(). This method goes through and adds
560 # all the remaining routes to the beginning of the list and ensures all
561 # the controllers have the right mixins.
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
562 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
563 # Anyway, if you are calling the URI dispatcher from outside of a
564 # Camping server, you'll definitely need to call this to set things up.
565 # Don't call it too early though. Any controllers added after this
566 # method is called won't work properly
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
567 def M
98e4333 * lib: hiding useless things in the docs.
_why authored Jul 12, 2006
568 def M #:nodoc:
569 end
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
570 constants.map { |c|
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
571 k = const_get(c)
596f092 @zimbatm Gained 7 octets :)
zimbatm authored Sep 23, 2007
572 k.send :include,C,Base,Helpers,Models
d485565 @zimbatm * Introduced error 501 handling and changed NotFound and ServerError …
zimbatm authored Oct 2, 2007
573 @r=[k]+r if r-[k]==r
3c19581 * lib/camping.rb: controller class names influence their urls. Contr…
_why authored Jun 24, 2008
574 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
575 }
576 end
8771109 * lib/camping.rb: protect ServerError and NotFound (from zimbatm's #67
_why authored Jul 6, 2006
577 end
578
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
579 # Internal controller with no route. Used to show internal messages.
b96ee93 @judofyr Some bytesaving and make class PostX match /post/([^/])
judofyr authored Jan 23, 2009
580 I = R()
abc7043 Initial import.
_why authored Jan 18, 2006
581 end
60c72e2 @zimbatm Small doc fixes. Especially X = module Controllers; self;end was brea…
zimbatm authored Jun 27, 2008
582 X = Controllers
abc7043 Initial import.
_why authored Jan 18, 2006
583
584 class << self
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
585 # When you are running many applications, you may want to create
586 # independent modules for each Camping application. Camping::goes
587 # defines a toplevel constant with the whole MVC rack inside:
61e9b19 lib/camping.rb: added Camping.goes, bug in routing.
_why authored Jan 21, 2006
588 #
589 # require 'camping'
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
590 # Camping.goes :Nuts
61e9b19 lib/camping.rb: added Camping.goes, bug in routing.
_why authored Jan 21, 2006
591 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
592 # module Nuts::Controllers; ... end
593 # module Nuts::Models; ... end
594 # module Nuts::Views; ... end
61e9b19 lib/camping.rb: added Camping.goes, bug in routing.
_why authored Jan 21, 2006
595 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
596 # All the applications will be available in Camping::Apps.
61e9b19 lib/camping.rb: added Camping.goes, bug in routing.
_why authored Jan 21, 2006
597 def goes(m)
5359649 @judofyr Earning some more bytes
judofyr authored Oct 29, 2008
598 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
599 end
d55d735 @judofyr First attempt to use Rack
judofyr authored May 20, 2008
600
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
601 # Ruby web servers use this method to enter the Camping realm. The +e+
60c72e2 @zimbatm Small doc fixes. Especially X = module Controllers; self;end was brea…
zimbatm authored Jun 27, 2008
602 # argument is the environment variables hash as per the Rack specification.
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
603 # And array with [status, headers, body] is expected at the output.
604 #
605 # See: http://rack.rubyforge.org/doc/SPEC.html
95d2262 @judofyr Synchronizing camping.rb and camping-unabridged.rb
judofyr authored May 24, 2008
606 def call(e)
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
607 X.M
332e0a1 @judofyr Make Camping::Session use Rack::Session::Cookie, this involves more c…
judofyr authored Jun 11, 2009
608 p = e['PATH_INFO'] = U.unescape(e['PATH_INFO'])
a7cff36 @judofyr Simplify things Rack gives us for free
judofyr authored Jun 11, 2009
609 k,m,*a=X.D p,e['REQUEST_METHOD'].downcase
64bd85c @judofyr Refactoring the exception handling
judofyr authored Jan 23, 2009
610 k.new(e,m).service(*a).to_a
611 rescue
612 r500(:I, k, m, $!, :env => e).to_a
abc7043 Initial import.
_why authored Jan 18, 2006
613 end
1109984 * lib/camping.rb: experimenting with Camping.method_missing as an an…
_why authored Jul 6, 2006
614
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
615 # The Camping scriptable dispatcher. Any unhandled method call to the app
616 # module will be sent to a controller class, specified as an argument.
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
617 #
618 # Blog.get(:Index)
619 # #=> #<Blog::Controllers::Index ... >
620 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
621 # The controller object contains all the @cookies, @body, @headers, etc.
622 # formulated by the response.
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
623 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
624 # You can also feed environment variables and query variables as a hash,
625 # the final argument.
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
626 #
627 # Blog.post(:Login, :input => {'username' => 'admin', 'password' => 'camping'})
628 # #=> #<Blog::Controllers::Login @user=... >
629 #
819f584 @zimbatm ActiveSupport dependency gone ! H is now simply inheriting from Hash.
zimbatm authored Sep 30, 2007
630 # Blog.get(:Info, :env => {'HTTP_HOST' => 'wagon'})
b09de27 @judofyr Removing @env - use @headers and @request instead
judofyr authored May 21, 2008
631 # #=> #<Blog::Controllers::Info @headers={'HTTP_HOST'=>'wagon'} ...>
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
632 #
1109984 * lib/camping.rb: experimenting with Camping.method_missing as an an…
_why authored Jul 6, 2006
633 def method_missing(m, c, *a)
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
634 X.M
64bd85c @judofyr Refactoring the exception handling
judofyr authored Jan 23, 2009
635 h = Hash === a[-1] ? a.pop : {}
ff6db3d @judofyr Allow setting more than :input in Camping.method_missing:
judofyr authored Jan 8, 2010
636 e = H[Rack::MockRequest.env_for('',h.delete(:env)||{})]
64bd85c @judofyr Refactoring the exception handling
judofyr authored Jan 23, 2009
637 k = X.const_get(c).new(e,m.to_s)
ff6db3d @judofyr Allow setting more than :input in Camping.method_missing:
judofyr authored Jan 8, 2010
638 h.each { |i, v| k.send("#{i}=", v) }
5093efa @zimbatm +2 octets for the sake of ruby -w strickness validity
zimbatm authored Sep 23, 2007
639 k.service(*a)
1109984 * lib/camping.rb: experimenting with Camping.method_missing as an an…
_why authored Jul 6, 2006
640 end
0d21c24 @judofyr Add Camping.use
judofyr authored Jun 11, 2009
641
642 # Injects a middleware:
643 #
644 # module Blog
645 # use Rack::MethodOverride
646 # use Rack::Session::Memcache, :key => "session"
647 # end
3be2a50 @judofyr Camping.use now also accepts a block
judofyr authored Nov 30, 2009
648 def use(*a, &b)
649 m = a.shift.new(method(:call), *a, &b)
0d21c24 @judofyr Add Camping.use
judofyr authored Jun 11, 2009
650 meta_def(:call) { |e| m.call(e) }
651 end
e3f70c5 @judofyr Introducing Camping.options
judofyr authored Apr 24, 2010
652
8abffe8 @judofyr Documentate Camping.options
judofyr authored Apr 24, 2010
653 # A hash where you can set different settings.
e3f70c5 @judofyr Introducing Camping.options
judofyr authored Apr 24, 2010
654 def options
655 O
656 end
8abffe8 @judofyr Documentate Camping.options
judofyr authored Apr 24, 2010
657
658 # Shortcut for setting options:
659 #
660 # module Blog
661 # set :secret, "Hello!"
662 # end
e3f70c5 @judofyr Introducing Camping.options
judofyr authored Apr 24, 2010
663 def set(k, v)
664 O[k] = v
665 end
abc7043 Initial import.
_why authored Jan 18, 2006
666 end
84604bd @judofyr Refactored some documentation; added lots of TODOs. It's not written …
judofyr authored Feb 14, 2009
667
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
668 # Views is an empty module for storing methods which create HTML. The HTML
5adca53 @judofyr Added some documentation of Camping::Views
judofyr authored Mar 7, 2009
669 # is described using the Markaby language.
670 #
671 # == Defining and calling templates
672 #
673 # Templates are simply Ruby methods with Markaby inside:
674 #
675 # module Blog::Views
676 # def index
677 # p "Welcome to my blog"
678 # end
679 #
680 # def show
681 # h1 @post.title
682 # self << @post.content
683 # end
684 # end
685 #
7b914ab @judofyr Some minor documentation cleanup
judofyr authored Mar 7, 2009
686 # In your controllers you just call <tt>render :template_name</tt> which will
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
687 # invoke the template. The views and controllers will share instance
5adca53 @judofyr Added some documentation of Camping::Views
judofyr authored Mar 7, 2009
688 # variables (as you can see above).
0777ceb @zimbatm Synched camping and unabridged by using `rake diff`. Used attr_reader…
zimbatm authored Sep 24, 2007
689 #
690 # == Using the layout method
691 #
5adca53 @judofyr Added some documentation of Camping::Views
judofyr authored Mar 7, 2009
692 # If your Views module has a <tt>layout</tt> method defined, it will be
693 # called with a block which will insert content from your view:
694 #
695 # module Blog::Views
696 # def layout
697 # html do
698 # head { title "My Blog "}
699 # body { self << yield }
700 # end
701 # end
702 # end
0777ceb @zimbatm Synched camping and unabridged by using `rake diff`. Used attr_reader…
zimbatm authored Sep 24, 2007
703 module Views; include X, Helpers end
84604bd @judofyr Refactored some documentation; added lots of TODOs. It's not written …
judofyr authored Feb 14, 2009
704
d3c7efe lib/camping.rb: trying to speed-up Markaby by creating the Mab class …
_why authored Jan 20, 2006
705 # Models is an empty Ruby module for housing model classes derived
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
706 # from ActiveRecord::Base. As a shortcut, you may derive from Base
d3c7efe lib/camping.rb: trying to speed-up Markaby by creating the Mab class …
_why authored Jan 20, 2006
707 # which is an alias for ActiveRecord::Base.
708 #
709 # module Camping::Models
710 # class Post < Base; belongs_to :user end
711 # class User < Base; has_many :posts end
712 # end
713 #
714 # == Where Models are Used
715 #
c40ee52 @judofyr The API-docs should be more precise and less tutorial-y
judofyr authored Oct 20, 2009
716 # Models are used in your controller classes. However, if your model class
d3c7efe lib/camping.rb: trying to speed-up Markaby by creating the Mab class …
_why authored Jan 20, 2006
717 # name conflicts with a controller class name, you will need to refer to it
718 # using the Models module.
719 #
720 # module Camping::Controllers
721 # class Post < R '/post/(\d+)'
722 # def get(post_id)
723 # @post = Models::Post.find post_id
724 # render :index
725 # end
726 # end
727 # end
728 #
729 # 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
730 module Models
a7be307 @judofyr Get rid Models.Y, was it ever used?
judofyr authored Nov 30, 2009
731 autoload :Base,'camping/ar'
fdbf7ed * lib/camping.rb: auto-prefix tables to prevent name clash between s…
_why authored Jan 25, 2006
732 end
0777ceb @zimbatm Synched camping and unabridged by using `rake diff`. Used attr_reader…
zimbatm authored Sep 24, 2007
733
6724a04 @zimbatm Removed Markaby as a direct dependency. As AR, it is loaded on demand.
zimbatm authored Oct 1, 2007
734 autoload :Mab, 'camping/mab'
760c4b7 @judofyr Add Tilt support (closes #18)
judofyr authored Apr 25, 2010
735 autoload :Template, 'camping/template'
5359649 @judofyr Earning some more bytes
judofyr authored Oct 29, 2008
736 C
abc7043 Initial import.
_why authored Jan 18, 2006
737 end
5723cd8 * lib/camping.rb: allow the Camping scriptable dispatcher (Camping.m…
_why authored Jul 12, 2006
738
Something went wrong with that request. Please try again.