Module: AsyncPartial::CaptureHelper

Defined in:
lib/async_partial.rb

Instance Method Summary collapse

Instance Method Details

#capture(*args, &block) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/async_partial.rb', line 17

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

#with_output_buffer(buf = nil) ⇒ Object

Simply rewind what’s written in the buffer



27
28
29
30
31
32
33
34
# File 'lib/async_partial.rb', line 27

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