Class: Arachni::Support::Buffer::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/arachni/support/buffer/base.rb

Overview

Base buffer class to be extended by more specialised implementation.

Author:

Direct Known Subclasses

AutoFlush

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_size = nil, type = Array) ⇒ Base

Returns a new instance of Base.

Parameters:

  • max_size (Integer) (defaults to: nil)

    Maximum buffer size – won’t be enforced.

  • type (#<<, #|, #clear, #size, #empty?) (defaults to: Array)

    Internal storage class to use.



34
35
36
37
38
39
40
41
# File 'lib/arachni/support/buffer/base.rb', line 34

def initialize( max_size = nil, type = Array )
    @buffer    = type.new
    @max_size  = max_size

    @on_flush_blocks      = []
    @on_push_blocks       = []
    @on_batch_push_blocks = []
end

Instance Attribute Details

#max_sizeInteger (readonly)

Returns Maximum buffer size.

Returns:

  • (Integer)

    Maximum buffer size.



28
29
30
# File 'lib/arachni/support/buffer/base.rb', line 28

def max_size
  @max_size
end

Instance Method Details

#<<(obj) ⇒ Object Also known as: push

Calls #on_push blocks with the given object and pushes an object to the buffer.

Parameters:

  • obj (Object)

    object to push



48
49
50
51
52
# File 'lib/arachni/support/buffer/base.rb', line 48

def <<( obj )
    call_on_push_blocks obj
    @buffer << obj
    self
end

#batch_push(list) ⇒ Object

Calls #on_batch_push blocks with the given list and merges the buffer with the contents of a list.

Parameters:

  • list (#|)

    list of objects



61
62
63
64
65
# File 'lib/arachni/support/buffer/base.rb', line 61

def batch_push( list )
    call_on_batch_push_blocks list
    @buffer |= list
    self
end

#empty?Bool

Returns ‘true` if the buffer is empty, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the buffer is empty, `false` otherwise



73
74
75
# File 'lib/arachni/support/buffer/base.rb', line 73

def empty?
    @buffer.empty?
end

#flushObject

Calls #on_flush blocks with the buffer and then empties it.

Returns:

  • current buffer



87
88
89
90
91
92
93
# File 'lib/arachni/support/buffer/base.rb', line 87

def flush
    buffer = @buffer.dup
    call_on_flush_blocks buffer
    buffer
ensure
    @buffer.clear
end

#full?Bool

Returns ‘true` if the buffer is full, `false` otherwise.

Returns:

  • (Bool)

    ‘true` if the buffer is full, `false` otherwise



78
79
80
# File 'lib/arachni/support/buffer/base.rb', line 78

def full?
    !!(max_size && size >= max_size)
end

#on_batch_push(&block) ⇒ Object

Parameters:



102
103
104
105
# File 'lib/arachni/support/buffer/base.rb', line 102

def on_batch_push( &block )
    @on_batch_push_blocks << block
    self
end

#on_flush(&block) ⇒ Object

Parameters:

  • block (Block)

    block to call on #flush



108
109
110
111
# File 'lib/arachni/support/buffer/base.rb', line 108

def on_flush( &block )
    @on_flush_blocks << block
    self
end

#on_push(&block) ⇒ Object

Parameters:

  • block (Block)

    block to call on #push



96
97
98
99
# File 'lib/arachni/support/buffer/base.rb', line 96

def on_push( &block )
    @on_push_blocks << block
    self
end

#sizeInteger

Returns amount of object in the buffer.

Returns:

  • (Integer)

    amount of object in the buffer



68
69
70
# File 'lib/arachni/support/buffer/base.rb', line 68

def size
    @buffer.size
end