wycats-merb-core 0.9.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (98) hide show
  1. data/CHANGELOG +992 -0
  2. data/CONTRIBUTORS +94 -0
  3. data/LICENSE +20 -0
  4. data/PUBLIC_CHANGELOG +142 -0
  5. data/README +21 -0
  6. data/Rakefile +458 -0
  7. data/TODO +0 -0
  8. data/bin/merb +11 -0
  9. data/bin/merb-specs +5 -0
  10. data/lib/merb-core.rb +598 -0
  11. data/lib/merb-core/autoload.rb +31 -0
  12. data/lib/merb-core/bootloader.rb +717 -0
  13. data/lib/merb-core/config.rb +305 -0
  14. data/lib/merb-core/constants.rb +45 -0
  15. data/lib/merb-core/controller/abstract_controller.rb +568 -0
  16. data/lib/merb-core/controller/exceptions.rb +315 -0
  17. data/lib/merb-core/controller/merb_controller.rb +256 -0
  18. data/lib/merb-core/controller/mime.rb +107 -0
  19. data/lib/merb-core/controller/mixins/authentication.rb +123 -0
  20. data/lib/merb-core/controller/mixins/conditional_get.rb +83 -0
  21. data/lib/merb-core/controller/mixins/controller.rb +319 -0
  22. data/lib/merb-core/controller/mixins/render.rb +513 -0
  23. data/lib/merb-core/controller/mixins/responder.rb +469 -0
  24. data/lib/merb-core/controller/template.rb +254 -0
  25. data/lib/merb-core/core_ext.rb +9 -0
  26. data/lib/merb-core/core_ext/hash.rb +7 -0
  27. data/lib/merb-core/core_ext/kernel.rb +340 -0
  28. data/lib/merb-core/dispatch/cookies.rb +130 -0
  29. data/lib/merb-core/dispatch/default_exception/default_exception.rb +93 -0
  30. data/lib/merb-core/dispatch/default_exception/views/_css.html.erb +198 -0
  31. data/lib/merb-core/dispatch/default_exception/views/_javascript.html.erb +73 -0
  32. data/lib/merb-core/dispatch/default_exception/views/index.html.erb +94 -0
  33. data/lib/merb-core/dispatch/dispatcher.rb +176 -0
  34. data/lib/merb-core/dispatch/request.rb +729 -0
  35. data/lib/merb-core/dispatch/router.rb +151 -0
  36. data/lib/merb-core/dispatch/router/behavior.rb +566 -0
  37. data/lib/merb-core/dispatch/router/cached_proc.rb +52 -0
  38. data/lib/merb-core/dispatch/router/resources.rb +191 -0
  39. data/lib/merb-core/dispatch/router/route.rb +511 -0
  40. data/lib/merb-core/dispatch/session.rb +222 -0
  41. data/lib/merb-core/dispatch/session/container.rb +74 -0
  42. data/lib/merb-core/dispatch/session/cookie.rb +173 -0
  43. data/lib/merb-core/dispatch/session/memcached.rb +68 -0
  44. data/lib/merb-core/dispatch/session/memory.rb +99 -0
  45. data/lib/merb-core/dispatch/session/store_container.rb +150 -0
  46. data/lib/merb-core/dispatch/worker.rb +28 -0
  47. data/lib/merb-core/gem_ext/erubis.rb +77 -0
  48. data/lib/merb-core/logger.rb +203 -0
  49. data/lib/merb-core/plugins.rb +67 -0
  50. data/lib/merb-core/rack.rb +25 -0
  51. data/lib/merb-core/rack/adapter.rb +44 -0
  52. data/lib/merb-core/rack/adapter/ebb.rb +25 -0
  53. data/lib/merb-core/rack/adapter/evented_mongrel.rb +26 -0
  54. data/lib/merb-core/rack/adapter/fcgi.rb +17 -0
  55. data/lib/merb-core/rack/adapter/irb.rb +118 -0
  56. data/lib/merb-core/rack/adapter/mongrel.rb +26 -0
  57. data/lib/merb-core/rack/adapter/runner.rb +28 -0
  58. data/lib/merb-core/rack/adapter/swiftiplied_mongrel.rb +26 -0
  59. data/lib/merb-core/rack/adapter/thin.rb +39 -0
  60. data/lib/merb-core/rack/adapter/thin_turbo.rb +24 -0
  61. data/lib/merb-core/rack/adapter/webrick.rb +36 -0
  62. data/lib/merb-core/rack/application.rb +32 -0
  63. data/lib/merb-core/rack/handler/mongrel.rb +97 -0
  64. data/lib/merb-core/rack/middleware.rb +20 -0
  65. data/lib/merb-core/rack/middleware/conditional_get.rb +29 -0
  66. data/lib/merb-core/rack/middleware/content_length.rb +18 -0
  67. data/lib/merb-core/rack/middleware/csrf.rb +73 -0
  68. data/lib/merb-core/rack/middleware/path_prefix.rb +31 -0
  69. data/lib/merb-core/rack/middleware/profiler.rb +19 -0
  70. data/lib/merb-core/rack/middleware/static.rb +45 -0
  71. data/lib/merb-core/rack/middleware/tracer.rb +20 -0
  72. data/lib/merb-core/server.rb +284 -0
  73. data/lib/merb-core/tasks/audit.rake +68 -0
  74. data/lib/merb-core/tasks/gem_management.rb +229 -0
  75. data/lib/merb-core/tasks/merb.rb +1 -0
  76. data/lib/merb-core/tasks/merb_rake_helper.rb +80 -0
  77. data/lib/merb-core/tasks/stats.rake +71 -0
  78. data/lib/merb-core/test.rb +11 -0
  79. data/lib/merb-core/test/helpers.rb +9 -0
  80. data/lib/merb-core/test/helpers/controller_helper.rb +8 -0
  81. data/lib/merb-core/test/helpers/multipart_request_helper.rb +175 -0
  82. data/lib/merb-core/test/helpers/request_helper.rb +393 -0
  83. data/lib/merb-core/test/helpers/route_helper.rb +39 -0
  84. data/lib/merb-core/test/helpers/view_helper.rb +121 -0
  85. data/lib/merb-core/test/matchers.rb +9 -0
  86. data/lib/merb-core/test/matchers/controller_matchers.rb +351 -0
  87. data/lib/merb-core/test/matchers/route_matchers.rb +137 -0
  88. data/lib/merb-core/test/matchers/view_matchers.rb +375 -0
  89. data/lib/merb-core/test/run_specs.rb +49 -0
  90. data/lib/merb-core/test/tasks/spectasks.rb +68 -0
  91. data/lib/merb-core/test/test_ext/hpricot.rb +32 -0
  92. data/lib/merb-core/test/test_ext/object.rb +14 -0
  93. data/lib/merb-core/test/test_ext/string.rb +14 -0
  94. data/lib/merb-core/vendor/facets.rb +2 -0
  95. data/lib/merb-core/vendor/facets/dictionary.rb +433 -0
  96. data/lib/merb-core/vendor/facets/inflect.rb +342 -0
  97. data/lib/merb-core/version.rb +3 -0
  98. metadata +253 -0
@@ -0,0 +1,513 @@
1
+ module Merb::RenderMixin
2
+ # So we can do raise TemplateNotFound
3
+ include Merb::ControllerExceptions
4
+
5
+ # ==== Parameters
6
+ # base<Module>:: Module that is including RenderMixin (probably a controller)
7
+ def self.included(base)
8
+ base.extend(ClassMethods)
9
+ base.class_eval do
10
+ class_inheritable_accessor :_default_render_options
11
+ end
12
+ end
13
+
14
+ module ClassMethods
15
+
16
+ # Return the default render options.
17
+ #
18
+ # ==== Returns
19
+ # Hash:: An options hash
20
+ def default_render_options
21
+ self._default_render_options ||= {}
22
+ end
23
+
24
+ # Set default render options at the class level.
25
+ #
26
+ # ==== Parameters
27
+ # opts<Hash>:: An options hash
28
+ def render_options(opts)
29
+ self._default_render_options = opts
30
+ end
31
+
32
+ # Set the default layout to use or nil/false to disable layout rendering.
33
+ # This is a shortcut for render_options :layout => false.
34
+ #
35
+ # ==== Parameters
36
+ # layout<~to_s>:: The layout that should be used for this class.
37
+ #
38
+ # ==== Notes
39
+ # You can override by passing :layout => true to render method.
40
+ #
41
+ # ==== Returns
42
+ # Hash:: The default render options.
43
+ def layout(layout)
44
+ self.default_render_options.update(:layout => (layout || false))
45
+ end
46
+
47
+ # Enable the default layout logic - reset the layout option.
48
+ def default_layout
49
+ self.default_render_options.delete(:layout)
50
+ end
51
+
52
+ end
53
+
54
+ # Render the specified item, with the specified options.
55
+ #
56
+ # ==== Parameters
57
+ # thing<String, Symbol, nil>::
58
+ # The thing to render. This will default to the current action
59
+ # opts<Hash>:: An options hash (see below)
60
+ #
61
+ # ==== Options (opts)
62
+ # :format<Symbol>:: A registered mime-type format
63
+ # :template<String>::
64
+ # The path to the template relative to the template root
65
+ # :status<~to_i>::
66
+ # The status to send to the client. Typically, this would be an integer
67
+ # (200), or a Merb status code (Accepted)
68
+ # :layout<~to_s, FalseClass>::
69
+ # A layout to use instead of the default. This should be relative to the
70
+ # layout root. By default, the layout will be either the controller_name or
71
+ # application. If you want to use an alternative content-type than the one
72
+ # that the base template was rendered as, you will need to do :layout =>
73
+ # "foo.#{content_type}" (i.e. "foo.json"). If you want to render without
74
+ # layout, use :layout => false. This overrides layout set by +layout+ method.
75
+ #
76
+ # ==== Returns
77
+ # String:: The rendered template, including layout, if appropriate.
78
+ #
79
+ # ==== Raises
80
+ # TemplateNotFound:: There is no template for the specified location.
81
+ #
82
+ # ==== Alternatives
83
+ # If you pass a Hash as the first parameter, it will be moved to opts and
84
+ # "thing" will be the current action
85
+ #---
86
+ # @public
87
+ def render(thing = nil, opts = {})
88
+ # render :format => :xml means render nil, :format => :xml
89
+ opts, thing = thing, nil if thing.is_a?(Hash)
90
+
91
+ # Merge with class level default render options
92
+ opts = self.class.default_render_options.merge(opts)
93
+
94
+ # If you don't specify a thing to render, assume they want to render the current action
95
+ thing ||= action_name.to_sym
96
+
97
+ # Content negotiation
98
+ self.content_type = opts[:format] if opts[:format]
99
+
100
+ # Handle options (:status)
101
+ _handle_options!(opts)
102
+
103
+ # Do we have a template to try to render?
104
+ if thing.is_a?(Symbol) || opts[:template]
105
+
106
+ template_method, template_location =
107
+ _template_for(thing, content_type, controller_name, opts[:template])
108
+
109
+ # Raise an error if there's no template
110
+ unless template_method && self.respond_to?(template_method)
111
+ template_files = Merb::Template.template_extensions.map { |ext| "#{template_location}.#{ext}" }
112
+ raise TemplateNotFound, "Oops! No template found. Merb was looking for #{template_files.join(', ')}" +
113
+ "for content type '#{content_type}'. You might have mispelled the template or file name. " +
114
+ "Registered template extensions: #{Merb::Template.template_extensions.join(', ')}. " +
115
+ "If you use Haml or some other template plugin, make sure you required Merb plugin dependency " +
116
+ "in your init file."
117
+ end
118
+
119
+ # Call the method in question and throw the content for later consumption by the layout
120
+ throw_content(:for_layout, self.send(template_method))
121
+
122
+ # Do we have a string to render?
123
+ elsif thing.is_a?(String)
124
+
125
+ # Throw it for later consumption by the layout
126
+ throw_content(:for_layout, thing)
127
+ end
128
+
129
+ # If we find a layout, use it. Otherwise, just render the content thrown for layout.
130
+ (layout = _get_layout(opts[:layout])) ? send(layout) : catch_content(:for_layout)
131
+ end
132
+
133
+ # Renders an object using to registered transform method based on the
134
+ # negotiated content-type, if a template does not exist. For instance, if the
135
+ # content-type is :json, Merb will first look for current_action.json.*.
136
+ # Failing that, it will run object.to_json.
137
+ #
138
+ # ==== Parameter
139
+ # object<Object>::
140
+ # An object that responds_to? the transform method registered for the
141
+ # negotiated mime-type.
142
+ # thing<String, Symbol>::
143
+ # The thing to attempt to render via #render before calling the transform
144
+ # method on the object. Defaults to nil.
145
+ # opts<Hash>::
146
+ # An options hash that will be used for rendering
147
+ # (passed on to #render or serialization methods like #to_json or #to_xml)
148
+ #
149
+ # ==== Returns
150
+ # String::
151
+ # The rendered template or if no template is found, the transformed object.
152
+ #
153
+ # ==== Raises
154
+ # NotAcceptable::
155
+ # If there is no transform method for the specified mime-type or the object
156
+ # does not respond to the transform method.
157
+ #
158
+ # ==== Alternatives
159
+ # A string in the second parameter will be interpreted as a template:
160
+ # display @object, "path/to/foo"
161
+ # #=> display @object, nil, :template => "path/to/foo"
162
+ #
163
+ # A hash in the second parameters will be interpreted as opts:
164
+ # display @object, :layout => "zoo"
165
+ # #=> display @object, nil, :layout => "zoo"
166
+ #
167
+ # If you need to pass extra parameters to serialization method, for instance,
168
+ # to exclude some of attributes or serialize associations, just pass options
169
+ # for it.
170
+ # For instance,
171
+ #
172
+ # display @locations, :except => [:locatable_type, :locatable_id], :include => [:locatable]
173
+ #
174
+ # serializes object with polymorphic association, not raw locatable_* attributes.
175
+ #
176
+ #
177
+ # ==== Options
178
+ #
179
+ # :template a template to use for rendering
180
+ # :layout a layout to use for rendering
181
+ # :status the status code to return (defaults to 200)
182
+ # :location the value of the Location header
183
+ #
184
+ # all other options options that will be pass to serialization method
185
+ # like #to_json or #to_xml
186
+ #
187
+ # ==== Notes
188
+ # The transformed object will not be used in a layout unless a :layout is
189
+ # explicitly passed in the opts.
190
+ #
191
+ def display(object, thing = nil, opts = {})
192
+ template_opt = thing.is_a?(Hash) ? thing.delete(:template) : opts.delete(:template)
193
+
194
+ case thing
195
+ # display @object, "path/to/foo" means display @object, nil, :template => "path/to/foo"
196
+ when String
197
+ template_opt, thing = thing, nil
198
+ # display @object, :template => "path/to/foo" means display @object, nil, :template => "path/to/foo"
199
+ when Hash
200
+ opts, thing = thing, nil
201
+ end
202
+
203
+ # Try to render without the object
204
+ render(thing || action_name.to_sym, opts.merge(:template => template_opt))
205
+
206
+ # If the render fails (i.e. a template was not found)
207
+ rescue TemplateNotFound => e
208
+ # Merge with class level default render options
209
+ # @todo can we find a way to refactor this out so we don't have to do it everywhere?
210
+ opts = self.class.default_render_options.merge(opts)
211
+
212
+ # Figure out what to transform and raise NotAcceptable unless there's a transform method assigned
213
+ transform = Merb.mime_transform_method(content_type)
214
+ if !transform
215
+ raise NotAcceptable, "#{e.message} and there was no transform method registered for #{content_type.inspect}"
216
+ elsif !object.respond_to?(transform)
217
+ raise NotAcceptable, "#{e.message} and your object does not respond to ##{transform}"
218
+ end
219
+
220
+ layout_opt = opts.delete(:layout)
221
+ _handle_options!(opts)
222
+ throw_content(:for_layout, opts.empty? ? object.send(transform) : object.send(transform, opts))
223
+
224
+ meth, _ = _template_for(layout_opt, layout_opt.to_s.index(".") ? nil : content_type, "layout") if layout_opt
225
+ meth ? send(meth) : catch_content(:for_layout)
226
+ end
227
+
228
+ # Render a partial template.
229
+ #
230
+ # ==== Parameters
231
+ # template<~to_s>::
232
+ # The path to the template, relative to the current controller or the
233
+ # template root; absolute path will work too. If the template contains a "/",
234
+ # Merb will search for it relative to the template root; otherwise,
235
+ # Merb will search for it relative to the current controller.
236
+ # opts<Hash>:: A hash of options (see below)
237
+ #
238
+ # ==== Options (opts)
239
+ # :with<Object, Array>::
240
+ # An object or an array of objects that will be passed into the partial.
241
+ # :as<~to_sym>:: The local name of the :with Object inside of the partial.
242
+ # :format<Symbol>:: The mime format that you want the partial to be in (:js, :html, etc.)
243
+ # others::
244
+ # A Hash object names and values that will be the local names and values
245
+ # inside the partial.
246
+ #
247
+ # ==== Examples
248
+ # partial :foo, :hello => @object
249
+ #
250
+ # The "_foo" partial will be called, relative to the current controller,
251
+ # with a local variable of +hello+ inside of it, assigned to @object.
252
+ #
253
+ # partial :bar, :with => ['one', 'two', 'three']
254
+ #
255
+ # The "_bar" partial will be called once for each element of the array
256
+ # specified by :with for a total of three iterations. Each element
257
+ # of the array will be available in the partial via a local variable named
258
+ # +bar+. Additionally, there will be two extra local variables:
259
+ # +collection_index+ and +collection_size+. +collection_index+ is the index
260
+ # of the object currently referenced by +bar+ in the collection passed to
261
+ # the partial. +collection_size+ is the total size of the collection.
262
+ #
263
+ # By default, the object specified by :with will be available through a
264
+ # local variable with the same name as the partial template. However,
265
+ # this can be changed using the :as option.
266
+ #
267
+ # partial :bar, :with => "one", :as => :number
268
+ #
269
+ # In this case, "one" will be available in the partial through the local
270
+ # variable named +number+.
271
+ #
272
+ # ==== Notes
273
+ # It is important to note that the object being passed to the partial
274
+ # as well as any extra local variables cannot use names of helper methods
275
+ # since any helper method of the same name will take precedence over the
276
+ # passed variable. Example:
277
+ #
278
+ # partial :bar, :with => "one", :as => :partial
279
+ #
280
+ # In this case, "one" will not be available in the partial because "partial"
281
+ # is already a helper method.
282
+ def partial(template, opts={})
283
+
284
+ # partial :foo becomes "#{controller_name}/_foo"
285
+ # partial "foo/bar" becomes "foo/_bar"
286
+ template = template.to_s
287
+ if template =~ %r{^/}
288
+ template_path = File.dirname(template) / "_#{File.basename(template)}"
289
+ else
290
+ kontroller = (m = template.match(/.*(?=\/)/)) ? m[0] : controller_name
291
+ template = "_#{File.basename(template)}"
292
+ end
293
+ template_method, template_location =
294
+ _template_for(template, opts.delete(:format) || content_type, kontroller, template_path)
295
+
296
+ (@_old_partial_locals ||= []).push @_merb_partial_locals
297
+
298
+ # This handles no :with as well
299
+ with = [opts.delete(:with)].flatten
300
+ as = opts.delete(:as) || template_location.match(%r[.*/_([^\.]*)])[1]
301
+
302
+ @_merb_partial_locals = opts.merge(:collection_index => -1, :collection_size => with.size)
303
+
304
+ # this handles an edge-case where the name of the partial is _foo.* and your opts
305
+ # have :foo as a key.
306
+ named_local = @_merb_partial_locals.key?(as.to_sym)
307
+
308
+ sent_template = with.map do |temp|
309
+ @_merb_partial_locals[as.to_sym] = temp unless named_local
310
+ if template_method && self.respond_to?(template_method)
311
+ @_merb_partial_locals[:collection_index] += 1
312
+ send(template_method)
313
+ else
314
+ raise TemplateNotFound, "Could not find template at #{template_location}.*"
315
+ end
316
+ end.join
317
+
318
+ @_merb_partial_locals = @_old_partial_locals.pop
319
+ sent_template
320
+ end
321
+
322
+ # Take the options hash and handle it as appropriate.
323
+ #
324
+ # ==== Parameters
325
+ # opts<Hash>:: The options hash that was passed into render.
326
+ #
327
+ # ==== Options
328
+ # :status<~to_i>::
329
+ # The status of the response will be set to opts[:status].to_i
330
+ #
331
+ # ==== Returns
332
+ # Hash:: The options hash that was passed in.
333
+ def _handle_options!(opts)
334
+ self.status = opts.delete(:status).to_i if opts[:status]
335
+ headers["Location"] = opts.delete(:location) if opts[:location]
336
+ opts
337
+ end
338
+
339
+ # Get the layout that should be used. The content-type will be appended to
340
+ # the layout unless the layout already contains a "." in it.
341
+ #
342
+ # If no layout was passed in, this method will look for one with the same
343
+ # name as the controller, and finally one in "application.#{content_type}".
344
+ #
345
+ # ==== Parameters
346
+ # layout<~to_s>:: A layout, relative to the layout root. Defaults to nil.
347
+ #
348
+ # ==== Returns
349
+ # String:: The method name that corresponds to the found layout.
350
+ #
351
+ # ==== Raises
352
+ # TemplateNotFound::
353
+ # If a layout was specified (either via layout in the class or by passing
354
+ # one in to this method), and not found. No error will be raised if no
355
+ # layout was specified, and the default layouts were not found.
356
+ def _get_layout(layout = nil)
357
+ return false if layout == false
358
+
359
+ layout = layout.instance_of?(Symbol) && self.respond_to?(layout, true) ? send(layout) : layout
360
+ layout = layout.to_s if layout
361
+
362
+ # If a layout was provided, throw an error if it's not found
363
+ if layout
364
+ template_method, template_location =
365
+ _template_for(layout, layout.index(".") ? nil : content_type, "layout")
366
+
367
+ raise TemplateNotFound, "No layout found at #{template_location}" unless template_method
368
+ template_method
369
+
370
+ # If a layout was not provided, try the default locations
371
+ else
372
+ template, location = _template_for(controller_name, content_type, "layout")
373
+ template, location = _template_for("application", content_type, "layout") unless template
374
+ template
375
+ end
376
+ end
377
+
378
+ # Iterate over the template roots in reverse order, and return the template
379
+ # and template location of the first match.
380
+ #
381
+ # ==== Parameters
382
+ # context<Object>:: The controller action or template (basename or absolute path).
383
+ # content_type<~to_s>:: The content type (like html or json).
384
+ # controller<~to_s>:: The name of the controller. Defaults to nil.
385
+ #
386
+ # ==== Options (opts)
387
+ # :template<String>::
388
+ # The location of the template to use. Defaults to whatever matches this
389
+ # context, content_type and controller.
390
+ #
391
+ # ==== Returns
392
+ # Array[Symbol, String]::
393
+ # A pair consisting of the template method and location.
394
+ def _template_for(context, content_type, controller=nil, template=nil)
395
+ template_method, template_location = nil, nil
396
+
397
+ # absolute path to a template (:template => "/foo/bar")
398
+ if template.is_a?(String) && template =~ %r{^/}
399
+ template_location = self._absolute_template_location(template, content_type)
400
+ return [_template_method_for(template_location), template_location]
401
+ end
402
+
403
+ self.class._template_roots.reverse_each do |root, template_meth|
404
+ # :template => "foo/bar.html" where root / "foo/bar.html.*" exists
405
+ if template
406
+ template_location = root / self.send(template_meth, template, content_type, nil)
407
+ # :layout => "foo" where root / "layouts" / "#{controller}.html.*" exists
408
+ else
409
+ template_location = root / self.send(template_meth, context, content_type, controller)
410
+ end
411
+
412
+ break if template_method = _template_method_for(template_location.to_s)
413
+ end
414
+
415
+ # template_location is a Pathname
416
+ [template_method, template_location.to_s]
417
+ end
418
+
419
+ # Return the template method for a location, and check to make sure the current controller
420
+ # actually responds to the method.
421
+ #
422
+ # ==== Parameters
423
+ # template_location<String>:: The phyical path of the template
424
+ #
425
+ # ==== Returns
426
+ # String:: The method, if it exists. Otherwise return nil.
427
+ def _template_method_for(template_location)
428
+ meth = Merb::Template.template_for(template_location)
429
+ meth && self.respond_to?(meth) ? meth : nil
430
+ end
431
+
432
+ # Called in templates to get at content thrown in another template. The
433
+ # results of rendering a template are automatically thrown into :for_layout,
434
+ # so catch_content or catch_content(:for_layout) can be used inside layouts
435
+ # to get the content rendered by the action template.
436
+ #
437
+ # ==== Parameters
438
+ # obj<Object>:: The key in the thrown_content hash. Defaults to :for_layout.
439
+ #---
440
+ # @public
441
+ def catch_content(obj = :for_layout)
442
+ @_caught_content[obj] || ''
443
+ end
444
+
445
+ # Called in templates to test for the existence of previously thrown content.
446
+ #
447
+ # ==== Parameters
448
+ # obj<Object>:: The key in the thrown_content hash. Defaults to :for_layout.
449
+ #---
450
+ # @public
451
+ def thrown_content?(obj = :for_layout)
452
+ @_caught_content.key?(obj)
453
+ end
454
+
455
+ # Called in templates to store up content for later use. Takes a string
456
+ # and/or a block. First, the string is evaluated, and then the block is
457
+ # captured using the capture() helper provided by the template languages. The
458
+ # two are concatenated together.
459
+ #
460
+ # ==== Parameters
461
+ # obj<Object>:: The key in the thrown_content hash.
462
+ # string<String>:: Textual content. Defaults to nil.
463
+ # &block:: A block to be evaluated and concatenated to string.
464
+ #
465
+ # ==== Raises
466
+ # ArgumentError:: Neither string nor block given.
467
+ #
468
+ # ==== Example
469
+ # throw_content(:foo, "Foo")
470
+ # catch_content(:foo) #=> "Foo"
471
+ #---
472
+ # @public
473
+ def throw_content(obj, string = nil, &block)
474
+ unless string || block_given?
475
+ raise ArgumentError, "You must pass a block or a string into throw_content"
476
+ end
477
+ @_caught_content[obj] = string.to_s << (block_given? ? capture(&block) : "")
478
+ end
479
+
480
+ # Called in templates to append content for later use. Works like throw_content.
481
+ #
482
+ # @param [Object] obj
483
+ # Key used in the thrown_content hash.
484
+ # @param [String] string
485
+ # Textual content. Default to nil.
486
+ # @yield
487
+ # Evaluated with result concatenated to string.
488
+ #
489
+ # @raise [ArgumentError]
490
+ # Neither string nor block given
491
+ #
492
+ # @api public
493
+ def append_content(obj, string = nil, &block)
494
+ unless string || block_given?
495
+ raise ArgumentError, "You must pass a block or a string into append_content"
496
+ end
497
+ @_caught_content[obj] = [] if @_caught_content[obj].nil?
498
+ @_caught_content[obj] << string.to_s << (block_given? ? capture(&block) : "")
499
+ end
500
+
501
+ # Called when renderers need to be sure that existing thrown content is cleared
502
+ # before throwing new content. This prevents double rendering of content when
503
+ # multiple templates are rendered after each other.
504
+ #
505
+ # ==== Parameters
506
+ # obj<Object>:: The key in the thrown_content hash. Defaults to :for_layout.
507
+ #---
508
+ # @public
509
+ def clear_content(obj = :for_layout)
510
+ @_caught_content.delete(obj) unless @_caught_content[obj].nil?
511
+ end
512
+
513
+ end