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
capture returns return value of passed in block if it is not a template block

Signed-off-by: Michael S. Klishin <michael@novemberain.com>
Drew Colthorp (author)
Wed Sep 24 18:58:52 -0700 2008
michaelklishin (committer)
Sat Sep 27 23:05:35 -0700 2008
commit  03ff7c4957115750d6802b9dee969be14a4b3057
tree    ace4ba2fc9e4d656eed17a208a3aece8757d6aba
parent  15ac66c1907732d254765c67551be42ef4a76d61
...
477
478
479
480
 
481
482
483
 
484
485
 
 
 
 
 
 
 
 
 
 
 
486
487
488
...
477
478
479
 
480
481
482
 
483
484
 
485
486
487
488
489
490
491
492
493
494
495
496
497
498
0
@@ -477,12 +477,22 @@ class Merb::AbstractController
0
   #
0
   # ==== Parameters
0
   # *args:: Arguments to pass to the block.
0
-  # &block:: The template block to call.
0
+  # &block:: The block to call.
0
   #
0
   # ==== Returns
0
-  # String:: The output of the block.
0
+  # String:: The output of a template block or the return value of a non-template block converted to a string.
0
   def capture(*args, &block)
0
-    send("capture_#{@_engine}", *args, &block)
0
+    ret = nil
0
+
0
+    captured = send("capture_#{@_engine}", *args) do |*args|
0
+      ret = yield *args
0
+    end
0
+
0
+    if captured.empty?
0
+      ret.to_s
0
+    else
0
+      captured
0
+    end
0
   end
0
 
0
   # Calls the concatenate method for the selected template engine.
...
19
20
21
 
 
 
 
 
 
22
23
24
...
19
20
21
22
23
24
25
26
27
28
29
30
0
@@ -19,6 +19,12 @@ module Merb::Test::Fixtures
0
     class CaptureWithArgs < HelperTesting
0
     end
0
     
0
+    class CaptureReturnValue < HelperTesting
0
+    end
0
+
0
+    class CaptureNonStringReturnValue < HelperTesting
0
+    end
0
+
0
     class CaptureEq < HelperTesting
0
       def helper_using_capture(&blk)
0
         "Beginning... #{capture(&blk)}... Done"
...
10
11
12
 
 
 
 
 
 
 
 
13
14
15
...
10
11
12
13
14
15
16
17
18
19
20
21
22
23
0
@@ -10,6 +10,14 @@ describe Merb::AbstractController, " with capture and concat" do
0
     dispatch_should_make_body("CaptureWithArgs", "Capture: one, two")
0
   end
0
 
0
+  it "should support capturing the return value of a non-template block" do
0
+    dispatch_should_make_body("CaptureReturnValue", "Capture")
0
+  end
0
+
0
+  it "should support capturing the return value of a non-template block" do
0
+    dispatch_should_make_body("CaptureNonStringReturnValue", "Captured ''")
0
+  end
0
+
0
   it "should support basic helpers that use capture with <%=" do
0
     dispatch_should_make_body("CaptureEq", "Pre. Beginning... Capturing... Done. Post.")
0
   end

Comments