class Metacrunch::Job::Buffer

Public Class Methods

new(size_or_proc) click to toggle source
# File lib/metacrunch/job/buffer.rb, line 4
def initialize(size_or_proc)
  @size_or_proc = size_or_proc
  @buffer = []

  if @size_or_proc.is_a?(Numeric) && @size_or_proc <= 0
    raise ArgumentError, "Buffer size must be a posive number greater that 0."
  end
end

Public Instance Methods

buffer(data) click to toggle source
# File lib/metacrunch/job/buffer.rb, line 13
def buffer(data)
  @buffer << data

  case @size_or_proc
  when Numeric
    flush if @buffer.count >= @size_or_proc.to_i
  when Proc
    flush if @size_or_proc.call == true
  end
end
flush() click to toggle source
# File lib/metacrunch/job/buffer.rb, line 24
def flush
  @buffer.presence
ensure
  @buffer = []
end