Module: Aerogel::Render::OutputBuffer

Defined in:
lib/aerogel/core/render/output_buffer.rb

Class Method Summary collapse

Class Method Details

.block_is_erb?(block) ⇒ Boolean

Returns true if block is defined within a ERb template.

Returns:

  • (Boolean)


48
49
50
# File 'lib/aerogel/core/render/output_buffer.rb', line 48

def self.block_is_erb?( block )
  !@buffer.nil? || block && eval('defined? __in_erb_template', block.binding)
end

.block_is_haml?(block) ⇒ Boolean

Returns true if block is defined within a ERb template.

Returns:

  • (Boolean)


54
55
56
# File 'lib/aerogel/core/render/output_buffer.rb', line 54

def self.block_is_haml?( block )
  block && eval('block_is_haml?(self)', block.binding)
end

.block_is_template?(block) ⇒ Boolean

Returns true if block is defined within any template.

Returns:

  • (Boolean)


60
61
62
# File 'lib/aerogel/core/render/output_buffer.rb', line 60

def self.block_is_template?( block )
  block && ( block_is_erb?(block) || block_is_haml?( block ) )
end

.bufferObject

Returns instance of a string buffer



9
10
11
# File 'lib/aerogel/core/render/output_buffer.rb', line 9

def self.buffer
  @buffer
end

.buffer=(text) ⇒ Object

Sets instance of a string buffer



15
16
17
# File 'lib/aerogel/core/render/output_buffer.rb', line 15

def self.buffer=( text )
  @buffer = text
end

.capture(*args, &block) ⇒ Object

Executes block and captures content rendered into buffer. If no content is rendered into buffer, returns block yield result as a content.



39
40
41
42
43
44
# File 'lib/aerogel/core/render/output_buffer.rb', line 39

def self.capture(*args, &block)
  self.push
  blk_result = yield *args
  buf_result = self.pop
  buf_result.nil? || buf_result.empty? ? blk_result : buf_result
end

.popObject

Returns current buffer contents as a string and restores previous buffer state from a stack.



30
31
32
33
34
# File 'lib/aerogel/core/render/output_buffer.rb', line 30

def self.pop
  result = @buffer
  @buffer = @buffer_stack.shift
  result
end

.pushObject

Saves current buffer in a stack and creates a new empty one.



21
22
23
24
25
# File 'lib/aerogel/core/render/output_buffer.rb', line 21

def self.push
  @buffer_stack ||= []
  @buffer_stack.unshift @buffer
  @buffer = ''
end