Class: Bitlogger::BufferDecorator

Inherits:
Object
  • Object
show all
Defined in:
lib/bitlogger/buffer_decorator.rb

Instance Method Summary collapse

Constructor Details

#initialize(target, method, max_size, max_time) ⇒ BufferDecorator

Returns a new instance of BufferDecorator.



3
4
5
6
7
8
9
10
11
12
# File 'lib/bitlogger/buffer_decorator.rb', line 3

def initialize(target, method, max_size, max_time)
  @target   = target
  @method   = method.to_sym
  @max_size = max_size
  @max_time = max_time
  @queue    = Queue.new

  create_alias
  start_consuming
end

Instance Method Details

#emptyObject



20
21
22
23
24
25
26
27
# File 'lib/bitlogger/buffer_decorator.rb', line 20

def empty
  @last_emptying = Time.now
  return if @queue.empty?

  to_send = []
  @queue.size.times { to_send << @queue.pop }
  @target.send(@method, to_send)
end

#push(msgs) ⇒ Object

Parameters:

  • msgs (Array<Object>, Object)


15
16
17
18
# File 'lib/bitlogger/buffer_decorator.rb', line 15

def push(msgs)
  msgs = [msgs] unless msgs.is_a?(Array)
  msgs.each { |msg| @queue << msg }
end