Module: AsyncPartial::CaptureHelper

Defined in:
lib/async_partial.rb

Instance Method Summary collapse

Instance Method Details

#capture(*args, &block) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/async_partial.rb', line 46

def capture(*args, &block)
  buf = Thread.current[:output_buffers].last
  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

#with_output_buffer(buf = nil) ⇒ Object

Simply rewind what’s written in the buffer



56
57
58
59
60
61
62
63
# File 'lib/async_partial.rb', line 56

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