Class: Cuboid::Support::Buffer::Base

Inherits:
Object
  • Object
show all
Includes:
Mixins::Observable
Defined in:
lib/cuboid/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

Methods included from Mixins::Observable

included

Methods included from Utilities

#available_port, available_port_mutex, #bytes_to_kilobytes, #bytes_to_megabytes, #caller_name, #caller_path, #exception_jail, #generate_token, #hms_to_seconds, #port_available?, #rand_port, #random_seed, #regexp_array_match, #remove_constants, #seconds_to_hms

Methods included from UI::Output

#error_buffer, initialize, #log_error, #output_provider_file, #print_bad, #print_debug, #print_error, #print_info, #print_line, #print_ok, #print_status, #print_verbose, #reroute_to_file, #reroute_to_file?

Methods included from UI::OutputInterface

initialize

Methods included from UI::OutputInterface::Personalization

#included

Methods included from UI::OutputInterface::Controls

#debug?, #debug_level, #debug_level_1?, #debug_level_2?, #debug_level_3?, #debug_level_4?, #debug_off, #debug_on, initialize, #verbose?, #verbose_off, #verbose_on

Methods included from UI::OutputInterface::ErrorLogging

#error_logfile, #has_error_log?, initialize, #set_error_logfile

Methods included from UI::OutputInterface::Implemented

#print_debug_backtrace, #print_debug_exception, #print_debug_level_1, #print_debug_level_2, #print_debug_level_3, #print_debug_level_4, #print_error_backtrace, #print_exception

Methods included from UI::OutputInterface::Abstract

#output_provider_file, #print_bad, #print_debug, #print_error, #print_info, #print_line, #print_ok, #print_status, #print_verbose

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.



31
32
33
34
35
# File 'lib/cuboid/support/buffer/base.rb', line 31

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

Instance Attribute Details

#max_sizeInteger (readonly)

Returns Maximum buffer size.

Returns:

  • (Integer)

    Maximum buffer size.



25
26
27
# File 'lib/cuboid/support/buffer/base.rb', line 25

def max_size
  @max_size
end

Instance Method Details

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

Note:

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

Parameters:

  • obj (Object)

    Object to push.



42
43
44
45
46
# File 'lib/cuboid/support/buffer/base.rb', line 42

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

#batch_push(list) ⇒ Object

Note:

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

Parameters:

  • list (#|)

    List of objects



54
55
56
57
58
# File 'lib/cuboid/support/buffer/base.rb', line 54

def batch_push( list )
    notify_on_batch_push 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.



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

def empty?
    @buffer.empty?
end

#flushObject

Note:

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

Returns current buffer.

Returns:

  • current buffer



81
82
83
84
85
86
87
# File 'lib/cuboid/support/buffer/base.rb', line 81

def flush
    buffer = @buffer.dup
    notify_on_flush 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.



74
75
76
# File 'lib/cuboid/support/buffer/base.rb', line 74

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

#on_batch_push(&block) ⇒ Object

Parameters:



18
# File 'lib/cuboid/support/buffer/base.rb', line 18

advertise :on_batch_push

#on_flush(&block) ⇒ Object

Parameters:

  • block (Block)

    block to call on #flush



22
# File 'lib/cuboid/support/buffer/base.rb', line 22

advertise :on_flush

#on_push(&block) ⇒ Object

Parameters:

  • block (Block)

    block to call on #push



14
# File 'lib/cuboid/support/buffer/base.rb', line 14

advertise :on_push

#sizeInteger

Returns Number of object in the buffer.

Returns:

  • (Integer)

    Number of object in the buffer.



62
63
64
# File 'lib/cuboid/support/buffer/base.rb', line 62

def size
    @buffer.size
end