aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_view
diff options
context:
space:
mode:
authorJeremy Kemper <jeremy@bitsweat.net>2009-03-13 02:36:00 -0700
committerJeremy Kemper <jeremy@bitsweat.net>2009-03-13 02:36:15 -0700
commit7c1714cbd0b37d1fc5ca2ac3e08980943454d516 (patch)
treec94b454f753281d9d9e67d6f0de237fa76f5417b /actionpack/lib/action_view
parent79b0b1a0ef926e91388e24bc77d3831f6d50b13d (diff)
downloadrails-7c1714cbd0b37d1fc5ca2ac3e08980943454d516.tar.gz
rails-7c1714cbd0b37d1fc5ca2ac3e08980943454d516.tar.bz2
rails-7c1714cbd0b37d1fc5ca2ac3e08980943454d516.zip
Body parts: future rendering, threaded future, queued future, open-uri example
Diffstat (limited to 'actionpack/lib/action_view')
-rw-r--r--actionpack/lib/action_view/base.rb10
-rw-r--r--actionpack/lib/action_view/body_parts/future.rb26
-rw-r--r--actionpack/lib/action_view/body_parts/open_uri.rb13
-rw-r--r--actionpack/lib/action_view/body_parts/queued.rb21
-rw-r--r--actionpack/lib/action_view/body_parts/threaded.rb21
5 files changed, 89 insertions, 2 deletions
diff --git a/actionpack/lib/action_view/base.rb b/actionpack/lib/action_view/base.rb
index e19acc5c29..3bbd2ca530 100644
--- a/actionpack/lib/action_view/base.rb
+++ b/actionpack/lib/action_view/base.rb
@@ -288,12 +288,12 @@ module ActionView #:nodoc:
# Access the current template being rendered.
# Returns a ActionView::Template object.
def template
- @_current_render
+ Thread.current[:_current_render]
end
def template=(template) #:nodoc:
@_first_render ||= template
- @_current_render = template
+ Thread.current[:_current_render] = template
end
def with_template(current_template)
@@ -303,6 +303,12 @@ module ActionView #:nodoc:
self.template = last_template
end
+ def punctuate_body!(part)
+ flush_output_buffer
+ response.body_parts << part
+ nil
+ end
+
private
# Evaluates the local assigns and controller ivars, pushes them to the view.
def _evaluate_assigns_and_ivars #:nodoc:
diff --git a/actionpack/lib/action_view/body_parts/future.rb b/actionpack/lib/action_view/body_parts/future.rb
new file mode 100644
index 0000000000..a5291c6e8c
--- /dev/null
+++ b/actionpack/lib/action_view/body_parts/future.rb
@@ -0,0 +1,26 @@
+module ActionView
+ module BodyParts
+ class Future
+ def initialize(&block)
+ @block = block
+ @parts = []
+ end
+
+ def to_s
+ finish
+ body
+ end
+
+ protected
+ def work
+ @block.call(@parts)
+ end
+
+ def body
+ str = ''
+ @parts.each { |part| str << part.to_s }
+ str
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_view/body_parts/open_uri.rb b/actionpack/lib/action_view/body_parts/open_uri.rb
new file mode 100644
index 0000000000..8ebd17b4a1
--- /dev/null
+++ b/actionpack/lib/action_view/body_parts/open_uri.rb
@@ -0,0 +1,13 @@
+require 'action_view/body_parts/threaded'
+require 'open-uri'
+
+module ActionView
+ module BodyParts
+ class OpenUri < Threaded
+ def initialize(url)
+ url = URI::Generic === url ? url : URI.parse(url)
+ super(true) { |parts| parts << url.read }
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_view/body_parts/queued.rb b/actionpack/lib/action_view/body_parts/queued.rb
new file mode 100644
index 0000000000..f8501f6a85
--- /dev/null
+++ b/actionpack/lib/action_view/body_parts/queued.rb
@@ -0,0 +1,21 @@
+require 'action_view/body_parts/future'
+
+module ActionView
+ module BodyParts
+ class Queued < Future
+ def initialize(job, &block)
+ super(&block)
+ enqueue(job)
+ end
+
+ protected
+ def enqueue(job)
+ @receipt = submit(job)
+ end
+
+ def finish
+ @parts << redeem(@receipt)
+ end
+ end
+ end
+end
diff --git a/actionpack/lib/action_view/body_parts/threaded.rb b/actionpack/lib/action_view/body_parts/threaded.rb
new file mode 100644
index 0000000000..34800bf9c7
--- /dev/null
+++ b/actionpack/lib/action_view/body_parts/threaded.rb
@@ -0,0 +1,21 @@
+require 'action_view/body_parts/future'
+
+module ActionView
+ module BodyParts
+ class Threaded < Future
+ def initialize(concurrent = false, &block)
+ super(&block)
+ concurrent ? start : work
+ end
+
+ protected
+ def start
+ @worker = Thread.new { work }
+ end
+
+ def finish
+ @worker.join if @worker && @worker.alive?
+ end
+ end
+ end
+end