public
Rubygem
Description: Merb Core: All you need. None you don't.
Homepage: http://www.merbivore.com
Clone URL: git://github.com/wycats/merb-core.git
Search Repo:
Merge branch 'master' of git@github.com:wycats/merb-core
ezmobius (author)
Wed May 14 17:34:25 -0700 2008
commit  5e1941211a2cde5e4d9f18c84251806b373ed49a
tree    23f938798bc64952075770760508d7abf4d10573
parent  085ce9721f52c9a56556341611f8ee943f5913ef parent  3d0c6563f267d8910f9b03a3fbc562095da2f44e
...
1
2
3
4
 
5
6
7
8
...
10
11
12
13
 
14
15
 
16
17
18
...
20
21
22
23
 
24
25
26
27
28
29
30
31
32
33
34
35
36
37
...
28
29
30
31
 
32
33
 
34
35
36
37
 
 
 
 
 
38
39
40
41
42
43
 
44
45
46
47
48
 
49
50
 
51
52
53
54
 
55
56
57
58
59
60
 
61
62
 
63
64
65
 
66
67
68
69
70
 
 
71
72
73
74
75
76
77
 
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
...
83
84
85
86
 
87
88
89
 
90
91
92
 
93
94
95
 
 
96
97
98
 
 
99
100
101
102
103
 
104
105
106
107
108
109
110
 
111
112
113
 
114
115
116
117
 
118
119
120
121
122
 
123
124
125
126
127
128
129
 
130
131
132
133
134
135
 
136
137
138
 
139
140
141
142
...
144
145
146
147
 
148
149
150
 
151
152
153
154
155
156
157
...
157
158
159
160
 
161
162
 
163
164
 
165
166
 
167
168
169
170
171
 
172
173
174
175
176
177
178
179
180
181
...
180
181
182
183
 
184
185
186
187
188
189
190
 
191
192
193
 
194
195
196
197
198
 
199
200
201
202
203
204
205
 
206
207
208
209
 
210
211
212
 
 
213
214
215
 
216
217
218
...
220
221
222
223
 
224
225
226
...
229
230
231
232
 
233
234
235
...
256
257
258
259
 
260
261
262
...
267
268
269
270
 
 
 
 
 
271
272
273
274
275
276
...
279
280
281
282
283
 
 
284
285
 
286
287
288
 
289
290
291
292
 
293
294
295
296
...
302
303
304
305
 
306
307
308
309
 
310
311
312
313
...
333
334
335
336
 
337
338
339
 
340
341
342
...
345
346
347
348
 
349
350
351
352
...
359
360
361
362
363
 
 
364
365
366
367
368
 
369
370
371
372
...
379
380
381
382
383
 
 
384
385
386
387
388
389
 
390
391
392
...
394
395
396
397
 
398
399
400
...
407
408
409
410
 
411
412
413
...
417
418
419
420
 
421
422
423
...
442
443
444
445
 
446
...
1
2
3
 
4
5
6
7
8
...
10
11
12
 
13
14
 
15
16
17
18
...
20
21
22
 
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
...
28
29
30
 
31
32
 
33
34
35
 
 
36
37
38
39
40
41
42
43
44
45
 
46
47
48
49
50
 
51
52
 
53
54
55
56
 
57
58
59
60
61
62
 
63
64
 
65
66
67
 
68
69
70
71
72
 
73
74
75
76
77
78
79
80
 
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
...
87
88
89
 
90
91
92
 
93
94
95
 
96
97
 
 
98
99
100
 
 
101
102
103
104
105
106
 
107
108
109
110
111
112
113
 
114
115
116
 
117
118
119
120
 
121
122
123
124
125
 
126
127
128
129
130
131
132
 
133
134
135
136
137
138
 
139
140
141
 
142
143
144
145
146
...
148
149
150
 
151
152
153
 
154
155
156
157
158
159
160
161
...
161
162
163
 
164
165
 
166
167
 
168
169
 
170
171
172
173
174
 
175
176
177
178
179
180
181
182
183
184
185
...
184
185
186
 
187
188
189
190
191
192
193
 
194
195
196
 
197
198
199
200
201
 
202
203
204
205
206
207
208
 
209
210
211
212
 
213
214
 
 
215
216
217
218
 
219
220
221
222
...
224
225
226
 
227
228
229
230
...
233
234
235
 
236
237
238
239
...
260
261
262
 
263
264
265
266
...
271
272
273
 
274
275
276
277
278
279
280
281
282
283
284
...
287
288
289
 
 
290
291
292
 
293
294
295
 
296
297
298
299
 
300
301
302
303
304
...
310
311
312
 
313
314
315
316
 
317
318
319
320
321
...
341
342
343
 
344
345
346
 
347
348
349
350
...
353
354
355
 
356
357
358
359
360
...
367
368
369
 
 
370
371
372
373
374
375
 
376
377
378
379
380
...
387
388
389
 
 
390
391
392
393
394
395
396
 
397
398
399
400
...
402
403
404
 
405
406
407
408
...
415
416
417
 
418
419
420
421
...
425
426
427
 
428
429
430
431
...
450
451
452
 
453
454
0
@@ -1,7 +1,7 @@
0
 module Merb::RenderMixin
0
   # So we can do raise TemplateNotFound
0
   include Merb::ControllerExceptions
0
-
0
+
0
   # ==== Parameters
0
   # base<Module>:: Module that is including RenderMixin (probably a controller)
0
   def self.included(base)
0
0
@@ -10,9 +10,9 @@
0
       class_inheritable_accessor :_default_render_options
0
     end
0
   end
0
-
0
+
0
   module ClassMethods
0
-
0
+
0
     # Return the default render options.
0
     #
0
     # ==== Returns
0
@@ -20,7 +20,7 @@
0
     def default_render_options
0
       self._default_render_options ||= {}
0
     end
0
-
0
+
0
     # Set default render options at the class level.
0
     #
0
     # ==== Parameters
0
0
0
0
0
0
0
0
0
0
0
0
@@ -28,53 +28,57 @@
0
     def render_options(opts)
0
       self._default_render_options = opts
0
     end
0
-
0
+
0
     # Set the default layout to use or nil/false to disable layout rendering.
0
- # This is a shortcut for render_options :layout => false.
0
+ # This is a shortcut for render_options :layout => false.
0
     #
0
     # ==== Parameters
0
- # layout<~to_s>:: The layout that should be used for this class
0
- #
0
+ # layout<~to_s>:: The layout that should be used for this class.
0
+ #
0
+ # ==== Notes
0
+ # You can override by passing :layout => true to render method.
0
+ #
0
     # ==== Returns
0
     # Hash:: The default render options.
0
     def layout(layout)
0
       self.default_render_options.update(:layout => (layout ? layout : false))
0
     end
0
-
0
+
0
     # Enable the default layout logic - reset the layout option.
0
     def default_layout
0
       self.default_render_options.delete(:layout)
0
     end
0
-
0
+
0
   end
0
-
0
+
0
   # Render the specified item, with the specified options.
0
   #
0
   # ==== Parameters
0
- # thing<String, Symbol, nil>::
0
+ # thing<String, Symbol, nil>::
0
   # The thing to render. This will default to the current action
0
   # opts<Hash>:: An options hash (see below)
0
   #
0
   # ==== Options (opts)
0
   # :format<Symbol>:: A registered mime-type format
0
- # :template<String>::
0
+ # :template<String>::
0
   # The path to the template relative to the template root
0
- # :status<~to_i>::
0
+ # :status<~to_i>::
0
   # The status to send to the client. Typically, this would be an integer
0
   # (200), or a Merb status code (Accepted)
0
- # :layout<~to_ss>::
0
+ # :layout<~to_s, FalseClass>::
0
   # A layout to use instead of the default. This should be relative to the
0
   # layout root. By default, the layout will be either the controller_name or
0
   # application. If you want to use an alternative content-type than the one
0
   # that the base template was rendered as, you will need to do :layout =>
0
- # "foo.#{content_type}" (i.e. "foo.json")
0
+ # "foo.#{content_type}" (i.e. "foo.json"). If you want to render without
0
+ # layout, use :layout => false. This overrides layout set by +layout+ method.
0
   #
0
   # ==== Returns
0
   # String:: The rendered template, including layout, if appropriate.
0
   #
0
   # ==== Raises
0
   # TemplateNotFound:: There is no template for the specified location.
0
- #
0
+ #
0
   # ==== Alternatives
0
   # If you pass a Hash as the first parameter, it will be moved to opts and
0
   # "thing" will be the current action
0
0
0
0
0
0
0
0
0
0
0
0
0
@@ -83,59 +87,59 @@
0
   def render(thing = nil, opts = {})
0
     # render :format => :xml means render nil, :format => :xml
0
     opts, thing = thing, nil if thing.is_a?(Hash)
0
-
0
+
0
     # Merge with class level default render options
0
     opts = self.class.default_render_options.merge(opts)
0
-
0
+
0
     # If you don't specify a thing to render, assume they want to render the current action
0
     thing ||= action_name.to_sym
0
-
0
+
0
     # Content negotiation
0
- opts[:format] ? (self.content_type = opts[:format]) : content_type
0
-
0
+ opts[:format] ? (self.content_type = opts[:format]) : content_type
0
+
0
     # Handle options (:status)
0
- _handle_options!(opts)
0
-
0
+ _handle_options!(opts)
0
+
0
     # Do we have a template to try to render?
0
     if thing.is_a?(Symbol) || opts[:template]
0
 
0
       template_method, template_location = _template_for(thing, content_type, controller_name, opts)
0
-
0
+
0
       # Raise an error if there's no template
0
       raise TemplateNotFound, "No template found at #{template_location}.*" \
0
         unless template_method && self.respond_to?(template_method)
0
 
0
       # Call the method in question and throw the content for later consumption by the layout
0
       throw_content(:for_layout, self.send(template_method))
0
-
0
+
0
     # Do we have a string to render?
0
     elsif thing.is_a?(String)
0
-
0
+
0
       # Throw it for later consumption by the layout
0
       throw_content(:for_layout, thing)
0
     end
0
-
0
+
0
     # If we find a layout, use it. Otherwise, just render the content thrown for layout.
0
     layout = opts[:layout] != false && _get_layout(opts[:layout])
0
     layout ? send(layout) : catch_content(:for_layout)
0
   end
0
-
0
+
0
   # Renders an object using to registered transform method based on the
0
   # negotiated content-type, if a template does not exist. For instance, if the
0
   # content-type is :json, Merb will first look for current_action.json.*.
0
   # Failing that, it will run object.to_json.
0
   #
0
   # ==== Parameter
0
- # object<Object>::
0
+ # object<Object>::
0
   # An object that responds_to? the transform method registered for the
0
   # negotiated mime-type.
0
   # thing<String, Symbol>::
0
   # The thing to attempt to render via #render before calling the transform
0
   # method on the object. Defaults to nil.
0
- # opts<Hash>::
0
+ # opts<Hash>::
0
   # An options hash that will be used for rendering
0
   # (passed on to #render or serialization methods like #to_json or #to_xml)
0
- #
0
+ #
0
   # ==== Returns
0
   # String::
0
   # The rendered template or if no template is found, the transformed object.
0
0
@@ -144,10 +148,10 @@
0
   # NotAcceptable::
0
   # If there is no transform method for the specified mime-type or the object
0
   # does not respond to the transform method.
0
- #
0
+ #
0
   # ==== Alternatives
0
   # A string in the second parameter will be interpreted as a template:
0
- # display @object, "path/to/foo"
0
+ # display @object, "path/to/foo"
0
   # #=> display @object, nil, :template => "path/to/foo"
0
   #
0
   # A hash in the second parameters will be interpreted as opts:
0
0
0
0
0
@@ -157,18 +161,18 @@
0
   # If you need to pass extra parameters to serialization method, for instance,
0
   # to exclude some of attributes or serialize associations, just pass options
0
   # for it.
0
- # For instance,
0
+ # For instance,
0
   #
0
- # display @locations, :except => [:locatable_type, :locatable_id], :include => [:locatable]
0
+ # display @locations, :except => [:locatable_type, :locatable_id], :include => [:locatable]
0
   #
0
- # serializes object with polymorphic association, not raw locatable_* attributes.
0
+ # serializes object with polymorphic association, not raw locatable_* attributes.
0
   #
0
- #
0
+ #
0
   # ==== Options
0
   #
0
   # :template a template to use for rendering
0
   # :layout a layout to use for rendering
0
-
0
+
0
   # all other options options that will be pass to serialization method
0
   # like #to_json or #to_xml
0
   #
0
0
0
0
0
0
0
0
@@ -180,39 +184,39 @@
0
     # display @object, "path/to/foo" means display @object, nil, :template => "path/to/foo"
0
     # display @object, :template => "path/to/foo" means display @object, nil, :template => "path/to/foo"
0
     template_opt = opts.delete(:template)
0
-
0
+
0
     case thing
0
     when String
0
       template_opt, thing = thing, nil
0
     when Hash
0
       opts, thing = thing, nil
0
     end
0
-
0
+
0
     # Try to render without the object
0
     render(thing || action_name.to_sym, opts.merge(:template => template_opt))
0
-
0
+
0
   # If the render fails (i.e. a template was not found)
0
   rescue TemplateNotFound
0
     # Merge with class level default render options
0
     opts = self.class.default_render_options.merge(opts)
0
-
0
+
0
     # Figure out what to transform and raise NotAcceptable unless there's a transform method assigned
0
     transform = Merb.mime_transform_method(content_type)
0
     raise NotAcceptable unless transform && object.respond_to?(transform)
0
 
0
     # Only use a layout if one was specified
0
     layout_opt = opts.delete(:layout)
0
-
0
+
0
     if layout_opt
0
       # Look for the layout under the default layout directly. If it's not found, reraise
0
       # the TemplateNotFound error
0
- template = _template_location(layout_opt, layout.index(".") ? content_type : nil, "layout")
0
+ template = _template_location(layout_opt, layout.index(".") ? content_type : nil, "layout")
0
       layout = _template_for(_template_root / template) ||
0
- (raise TemplateNotFound, "No layout found at #{_template_root / template}.*")
0
-
0
+ (raise TemplateNotFound, "No layout found at #{_template_root / template}.*")
0
+
0
       # If the layout was found, call it
0
       send(layout)
0
-
0
+
0
     # Otherwise, just render the transformed object
0
     else
0
       unless opts.empty?
0
@@ -220,7 +224,7 @@
0
         throw_content(:for_layout, object.send(transform, opts))
0
       else
0
         throw_content(:for_layout, object.send(transform))
0
- end
0
+ end
0
       catch_content(:for_layout)
0
     end
0
   end
0
@@ -229,7 +233,7 @@
0
   #
0
   # ==== Parameters
0
   # template<~to_s>::
0
- # The path to the template, relative to the current controller or the
0
+ # The path to the template, relative to the current controller or the
0
   # template root. If the template contains a "/", Merb will search for it
0
   # relative to the template root; otherwise, Merb will search for it
0
   # relative to the current controller.
0
@@ -256,7 +260,7 @@
0
     template = template.to_s
0
     kontroller = (m = template.match(/.*(?=\/)/)) ? m[0] : controller_name
0
     template = "_#{File.basename(template)}"
0
-
0
+
0
     template_method, template_location = _template_for(template, opts.delete(:format) || content_type, kontroller)
0
 
0
     (@_old_partial_locals ||= []).push @_merb_partial_locals
0
@@ -267,7 +271,11 @@
0
       @_merb_partial_locals = opts
0
       sent_template = [with].flatten.map do |temp|
0
         @_merb_partial_locals[as.to_sym] = temp
0
- send(template_method)
0
+ if template_method && self.respond_to?(template_method)
0
+ send(template_method)
0
+ else
0
+ raise TemplateNotFound, "Could not find template at #{template_location}.*"
0
+ end
0
       end.join
0
     else
0
       @_merb_partial_locals = opts
0
0
0
0
@@ -279,17 +287,17 @@
0
     end
0
     @_merb_partial_locals = @_old_partial_locals.pop
0
     sent_template
0
- end
0
-
0
+ end
0
+
0
   # Take the options hash and handle it as appropriate.
0
- #
0
+ #
0
   # ==== Parameters
0
   # opts<Hash>:: The options hash that was passed into render.
0
- #
0
+ #
0
   # ==== Options
0
   # :status<~to_i>::
0
   # The status of the response will be set to opts[:status].to_i
0
- #
0
+ #
0
   # ==== Returns
0
   # Hash:: The options hash that was passed in.
0
   def _handle_options!(opts)
0
0
@@ -302,11 +310,11 @@
0
   #
0
   # ==== Parameters
0
   # opts<Hash>:: The options hash that was passed to the render
0
- #
0
+ #
0
   # ==== Options
0
   # :location
0
   # Sets headers['Location'] to the provided URL
0
- #
0
+ #
0
   # ==== Returns
0
   # Hash:: The options hash that was passed in.
0
   def _handle_location!(opts)
0
0
@@ -333,10 +341,10 @@
0
   #
0
   # ==== Parameters
0
   # layout<~to_s>:: A layout, relative to the layout root. Defaults to nil.
0
- #
0
+ #
0
   # ==== Returns
0
   # String:: The method name that corresponds to the found layout.
0
- #
0
+ #
0
   # ==== Raises
0
   # TemplateNotFound::
0
   # If a layout was specified (either via layout in the class or by passing
0
@@ -345,7 +353,7 @@
0
   def _get_layout(layout = nil)
0
     layout = layout.instance_of?(Symbol) && self.respond_to?(layout, true) ? send(layout) : layout
0
     layout = layout.to_s if layout
0
-
0
+
0
     # If a layout was provided, throw an error if it's not found
0
     if layout
0
       template_method, template_location = _template_for(layout, layout.index(".") ? nil : content_type, "layout")
0
0
@@ -359,13 +367,13 @@
0
       template
0
     end
0
   end
0
-
0
- # Iterate over the template roots in reverse order, and return the template
0
+
0
+ # Iterate over the template roots in reverse order, and return the template
0
   # and template location of the first match.
0
   #
0
   # ==== Parameters
0
   # context<Object>:: The controller action or template basename.
0
- # content_type<~to_s>:: The content type. Defaults to nil.
0
+ # content_type<~to_s>:: The content type (like html or json).
0
   # controller<~to_s>:: The name of the controller. Defaults to nil.
0
   #
0
   # ==== Options (opts)
0
0
@@ -379,14 +387,14 @@
0
   def _template_for(context, content_type, controller=nil, opts={})
0
     template_method = nil
0
     template_location = nil
0
-
0
- self.class._template_roots.reverse_each do |root, template_location|
0
+
0
+ self.class._template_roots.reverse_each do |root, template_location|
0
       if opts[:template] # use the given template as the location context
0
         template_location = root / self.send(template_location, opts[:template], content_type, nil)
0
         template_method = Merb::Template.template_for(template_location)
0
         break if template_method && self.respond_to?(template_method)
0
       end
0
-
0
+
0
       template_location = root / (opts[:template] || self.send(template_location, context, content_type, controller))
0
       template_method = Merb::Template.template_for(template_location)
0
       break if template_method && self.respond_to?(template_method)
0
@@ -394,7 +402,7 @@
0
 
0
     [template_method, template_location]
0
   end
0
-
0
+
0
   # Called in templates to get at content thrown in another template. The
0
   # results of rendering a template are automatically thrown into :for_layout,
0
   # so catch_content or catch_content(:for_layout) can be used inside layouts
0
@@ -407,7 +415,7 @@
0
   def catch_content(obj = :for_layout)
0
     @_caught_content[obj]
0
   end
0
-
0
+
0
   # Called in templates to test for the existence of previously thrown content.
0
   #
0
   # ==== Parameters
0
@@ -417,7 +425,7 @@
0
   def thrown_content?(obj = :for_layout)
0
     @_caught_content.key?(obj)
0
   end
0
-
0
+
0
   # Called in templates to store up content for later use. Takes a string
0
   # and/or a block. First, the string is evaluated, and then the block is
0
   # captured using the capture() helper provided by the template languages. The
0
@@ -442,6 +450,6 @@
0
     end
0
     @_caught_content[obj] = string.to_s << (block_given? ? capture(&block) : "")
0
   end
0
-
0
+
0
 end
...
113
114
115
 
 
 
 
 
 
116
117
118
119
 
 
120
121
122
...
113
114
115
116
117
118
119
120
121
122
123
124
 
125
126
127
128
129
0
@@ -113,10 +113,17 @@
0
 
0
     # ==== Returns
0
     # Hash:: Parameters from body if this is a JSON request.
0
+ #
0
+ # ==== Notes
0
+ # If the JSON object parses as a Hash, it will be merged with the
0
+ # parameters hash. If it parses to anything else (such as an Array, or
0
+ # if it inflates to an Object) it will be accessible via the inflated_object
0
+ # parameter.
0
     def json_params
0
       @json_params ||= begin
0
         if Merb::Const::JSON_MIME_TYPE_REGEXP.match(content_type)
0
- JSON.parse(raw_post)
0
+ jobj = JSON.parse(raw_post)
0
+ jobj.kind_of?(Hash) ? jobj : { :inflated_object => jobj }
0
         end
0
       end
0
     end
...
1
2
 
3
4
5
6
7
8
9
10
11
12
13
14
 
15
16
 
17
18
19
20
21
22
 
23
24
25
26
27
28
 
29
30
31
32
33
34
 
35
36
37
38
39
40
 
41
42
43
44
45
46
 
47
...
1
 
2
3
4
5
6
7
 
8
9
10
11
12
 
13
14
 
15
16
17
18
 
19
 
20
21
22
23
24
25
 
26
27
28
29
30
31
 
32
33
34
35
36
37
 
38
39
40
41
42
43
 
44
45
0
@@ -1,48 +1,46 @@
0
 module Merb::Test::Fixtures::Controllers
0
-
0
+
0
   class Testing < Merb::Controller
0
     self._template_root = File.dirname(__FILE__) / "views"
0
   end
0
 
0
   class BasicAuthentication < Testing
0
-
0
     before :authenticate, :only => :index
0
 
0
     def index
0
       "authenticated"
0
     end
0
-
0
+
0
     protected
0
-
0
+
0
     def authenticate
0
       basic_authentication { |u, p| u == "Fred" && p == "secret" }
0
     end
0
-
0
   end
0
-
0
+
0
   class BasicAuthenticationWithRealm < BasicAuthentication
0
     def authenticate
0
       basic_authentication("My Super App") { |u, p| u == "Fred" && p == "secret" }
0
     end
0
   end
0
-
0
+
0
   class AuthenticateBasicAuthentication < Testing
0
     def index
0
       basic_authentication.authenticate { |u, p| "Fred:secret" }
0
     end
0
   end
0
-
0
+
0
   class RequestBasicAuthentication < BasicAuthentication
0
     def authenticate
0
       basic_authentication.request
0
     end
0
   end
0
-
0
+
0
   class RequestBasicAuthenticationWithRealm < BasicAuthentication
0
     def authenticate
0
       basic_authentication("My SuperApp").request
0
     end
0
   end
0
-
0
+
0
 end
...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
...
21
22
23
24
25
26
27
28
29
 
30
31
32
...
 
 
1
 
2
 
3
4
5
6
7
 
8
9
10
11
...
16
17
18
 
19
20
21
22
 
23
24
25
26
0
@@ -1,15 +1,10 @@
0
-
0
-
0
 module Merb::Test::Fixtures
0
-
0
   module Controllers
0
-
0
     class Testing < Merb::Controller
0
       self._template_root = File.dirname(__FILE__) / "views"
0
     end
0
 
0
     module Inclusion
0
-
0
       def self.included(base)
0
         base.show_action(:baz)
0
       end
0
0
@@ -21,12 +16,11 @@
0
       def bat
0
         "bat"
0
       end
0
-
0
     end
0
 
0
     class Base < Testing
0
       include Inclusion
0
-
0
+
0
       def index
0
         self.status = :ok
0
         "index"