Class: ViewComponent::OutputBufferStack

Inherits:
Object
  • Object
show all
Defined in:
lib/view_component/output_buffer_stack.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(initial_buffer = nil) ⇒ OutputBufferStack

Returns a new instance of OutputBufferStack.



14
15
16
17
18
19
20
21
22
# File 'lib/view_component/output_buffer_stack.rb', line 14

def initialize(initial_buffer = nil)
  if initial_buffer.is_a?(self.class)
    @current_buffer = self.class.make_frame(initial_buffer.current)
    @buffer_stack = [*initial_buffer.buffer_stack[0..-2], @current_buffer]
  else
    @current_buffer = initial_buffer || self.class.make_frame
    @buffer_stack = [@current_buffer]
  end
end

Instance Attribute Details

#buffer_stackObject (readonly)

Returns the value of attribute buffer_stack.



8
9
10
# File 'lib/view_component/output_buffer_stack.rb', line 8

def buffer_stack
  @buffer_stack
end

Class Method Details

.make_frame(*args) ⇒ Object



10
11
12
# File 'lib/view_component/output_buffer_stack.rb', line 10

def self.make_frame(*args)
  ActionView::OutputBuffer.new(*args)
end

Instance Method Details

#append=(arg) ⇒ Object



31
32
33
# File 'lib/view_component/output_buffer_stack.rb', line 31

def append=(arg)
  @current_buffer.append = arg
end

#lengthObject



43
44
45
# File 'lib/view_component/output_buffer_stack.rb', line 43

def length
  @current_buffer.length
end

#popObject



53
54
55
56
57
# File 'lib/view_component/output_buffer_stack.rb', line 53

def pop
  @buffer_stack.pop.tap do
    @current_buffer = @buffer_stack.last
  end
end

#push(buffer = nil) ⇒ Object



47
48
49
50
51
# File 'lib/view_component/output_buffer_stack.rb', line 47

def push(buffer = nil)
  buffer ||= self.class.make_frame
  @buffer_stack.push(buffer)
  @current_buffer = buffer
end

#replace(buffer) ⇒ Object



24
25
26
27
28
29
# File 'lib/view_component/output_buffer_stack.rb', line 24

def replace(buffer)
  return if self == buffer

  @current_buffer = buffer.current
  @buffer_stack = buffer.buffer_stack
end

#safe_append=(arg) ⇒ Object



35
36
37
# File 'lib/view_component/output_buffer_stack.rb', line 35

def safe_append=(arg)
  @current_buffer.safe_append = arg
end

#safe_concat(arg) ⇒ Object



39
40
41
# File 'lib/view_component/output_buffer_stack.rb', line 39

def safe_concat(arg)
  @current_buffer.safe_concat(arg)
end

#to_sObject Also known as: current



59
60
61
# File 'lib/view_component/output_buffer_stack.rb', line 59

def to_s
  @current_buffer
end