lib/async_partial.rb in async_partial-0.3.0 vs lib/async_partial.rb in async_partial-0.4.0
- old
+ new
@@ -11,10 +11,31 @@
super
end
end
end
+ module CaptureHelper
+ def capture(*args, &block)
+ buf = block.binding.local_variable_get :output_buffer
+ value = nil
+ buffer = with_output_buffer(buf) { value = block.call(*args) }
+ if (string = buffer.presence || value) && string.is_a?(String)
+ ERB::Util.html_escape string
+ end
+ end
+
+ # Simply rewind what's written in the buffer
+ def with_output_buffer(buf = nil) #:nodoc:
+ buffer_values_was = buf.buffer_values.clone
+ yield
+ buffer_values_was.each {|e| buf.buffer_values.shift if buf.buffer_values[0] == e}
+ buf.to_s
+ ensure
+ buf.buffer_values = buffer_values_was
+ end
+ end
+
class AsyncResult
def initialize(thread)
@thread = thread
end
@@ -36,34 +57,36 @@
val
end
end
module ArrayBuffer
+ attr_accessor :buffer_values
+
def initialize(*)
super
- @values = []
+ @buffer_values = []
end
def <<(value)
- @values << [value, :<<] unless value.nil?
+ @buffer_values << [value, :<<] unless value.nil?
self
end
alias :append= :<<
def safe_concat(value)
raise ActiveSupport::SafeBuffer::SafeConcatError unless html_safe?
- @values << [value, :safe_concat] unless value.nil?
+ @buffer_values << [value, :safe_concat] unless value.nil?
self
end
alias :safe_append= :safe_concat
def safe_expr_append=(val)
- @values << [val, :safe_expr_append] unless val.nil?
+ @buffer_values << [val, :safe_expr_append] unless val.nil?
self
end
def to_s
- result = @values.each_with_object(ActiveSupport::SafeBuffer.new) do |(v, meth), buf|
+ result = @buffer_values.each_with_object(ActiveSupport::SafeBuffer.new) do |(v, meth), buf|
if meth == :<<
if AsyncPartial::AsyncResult === v
buf << v.value
else
buf << v.to_s