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



45
46
47
# File 'lib/view_component/output_buffer_stack.rb', line 45

def length
  @current_buffer.length
end

#popObject



55
56
57
58
59
# File 'lib/view_component/output_buffer_stack.rb', line 55

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

#push(buffer = nil) ⇒ Object



49
50
51
52
53
# File 'lib/view_component/output_buffer_stack.rb', line 49

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
42
43
# File 'lib/view_component/output_buffer_stack.rb', line 39

def safe_concat(arg)
  # rubocop:disable Rails/OutputSafety
  @current_buffer.safe_concat(arg)
  # rubocop:enable Rails/OutputSafety
end

#to_sObject Also known as: current



61
62
63
# File 'lib/view_component/output_buffer_stack.rb', line 61

def to_s
  @current_buffer
end