Class: HQ::LogMonitorClient::Script::ContextReader

Inherits:
Object
  • Object
show all
Defined in:
lib/hq/log-monitor-client/script.rb

Instance Method Summary collapse

Constructor Details

#initialize(source, buffer_size) ⇒ ContextReader

Returns a new instance of ContextReader.



366
367
368
369
370
371
372
373
374
375
# File 'lib/hq/log-monitor-client/script.rb', line 366

def initialize source, buffer_size

	@source = source
	@buffer_size = buffer_size

	@buffer = Array.new @buffer_size

	reset

end

Instance Method Details

#getsObject



428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# File 'lib/hq/log-monitor-client/script.rb', line 428

def gets

	# make sure the next line is in the buffer

	if lines_after_count == 0
		read_next_line or return nil
	end

	# return the line, advancing the cursor

	ret = @buffer[@buffer_cursor % @buffer_size]
	@buffer_cursor += 1
	return ret

end

#last_line_numberObject



444
445
446
447
# File 'lib/hq/log-monitor-client/script.rb', line 444

def last_line_number
	raise "No last line" unless @buffer_cursor > 0
	@buffer_cursor - 1
end

#lines_after(count) ⇒ Object



392
393
394
395
396
397
398
399
400
401
# File 'lib/hq/log-monitor-client/script.rb', line 392

def lines_after count
	count = [ count, @buffer_size ].min
	while lines_after_count < count
		read_next_line or break
	end
	count = [ count, @buffer_end - @buffer_cursor].min
	return (0...count).map {
		|i| @buffer[(@buffer_cursor + i) % @buffer_size]
	}
end

#lines_after_countObject



381
382
383
# File 'lib/hq/log-monitor-client/script.rb', line 381

def lines_after_count
	return @buffer_end - @buffer_cursor
end

#lines_before(count) ⇒ Object



385
386
387
388
389
390
# File 'lib/hq/log-monitor-client/script.rb', line 385

def lines_before count
	count = [ count, lines_before_count ].min
	return (0...count).map {
		|i| @buffer[(@buffer_cursor - count + i) % @buffer_size]
	}
end

#lines_before_countObject



377
378
379
# File 'lib/hq/log-monitor-client/script.rb', line 377

def lines_before_count
	return @buffer_cursor - @buffer_start
end

#next_line_numberObject



449
450
451
# File 'lib/hq/log-monitor-client/script.rb', line 449

def next_line_number
	@buffer_cursor
end

#read_next_lineObject



403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'lib/hq/log-monitor-client/script.rb', line 403

def read_next_line

	# read a line

	line = @source.gets
	return false unless line

	line.strip!
	line.freeze

	# shrink buffer if full

	if @buffer_end - @buffer_start == @buffer_size
		@buffer_start += 1
	end

	# add line to buffer

	@buffer[@buffer_end % @buffer_size] = line
	@buffer_end += 1

	return true

end

#resetObject



453
454
455
456
457
# File 'lib/hq/log-monitor-client/script.rb', line 453

def reset
	@buffer_start = 0
	@buffer_cursor = 0
	@buffer_end = 0
end