mirror of
https://github.com/meineerde/rackstash.git
synced 2025-10-17 14:01:01 +00:00
Add BufferStack#pop to remove the top-most Buffer without flushing it
This can be used for certain advanced constructs where the Buffer needs to be manually flushed later, after it was already removed from the stack.
This commit is contained in:
parent
0fa327bb3a
commit
a987e92250
@ -49,13 +49,18 @@ module Rackstash
|
||||
buffer
|
||||
end
|
||||
|
||||
# Remove the top-most Buffer from the internal stack.
|
||||
# Remove the top-most {Buffer} from the internal stack without flushing it.
|
||||
#
|
||||
# If there was a buffer on the stack and it has pending data, it is flushed
|
||||
# to the {#sink} before it is returned.
|
||||
# @return [Buffer, nil] the poped {Buffer} or `nil` if there was no {Buffer}
|
||||
# to remove.
|
||||
def pop
|
||||
@stack.pop
|
||||
end
|
||||
|
||||
# Flush and remove the top-most {Buffer} from the internal stack.
|
||||
#
|
||||
# @return [Buffer, nil] the removed {Buffer} or `nil` if there was no
|
||||
# {Buffer} to remove
|
||||
# @return [Buffer, nil] the poped and flushed {Buffer} or `nil` if there was
|
||||
# no {Buffer} to remove
|
||||
def flush_and_pop
|
||||
buffer = @stack.pop
|
||||
buffer.flush if buffer
|
||||
|
||||
@ -85,4 +85,30 @@ describe Rackstash::BufferStack do
|
||||
stack.flush_and_pop # no further buffer, thus `#flush` is not called again
|
||||
end
|
||||
end
|
||||
|
||||
describe '#pop' do
|
||||
it 'removes a buffer from the stack' do
|
||||
stack.push
|
||||
expect { stack.pop }
|
||||
.to change { stack.instance_variable_get(:@stack).count }.from(1).to(0)
|
||||
end
|
||||
|
||||
it 'does nothing if there is no buffer' do
|
||||
expect(stack.instance_variable_get(:@stack).count).to eql 0
|
||||
expect(stack.pop).to be_nil
|
||||
end
|
||||
|
||||
it 'returns the pop\'ed Buffer' do
|
||||
stack.push
|
||||
expect(stack.pop).to be_a Rackstash::Buffer
|
||||
expect(stack.pop).to be nil
|
||||
end
|
||||
|
||||
it 'does not flushes the buffer' do
|
||||
new_buffer = stack.push
|
||||
|
||||
expect(new_buffer).not_to receive(:flush)
|
||||
stack.pop
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user