Sha256: ae8891ef3862a9b1796a91205f1da72be13c4a20a32c0682502d5d8cff9dbe87

Contents?: true

Size: 1.19 KB

Versions: 6

Compression:

Stored size: 1.19 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2023, by Samuel Williams.

require_relative 'filter'

module Console
	# A general sink which captures all events into a buffer.
	class Capture
		def initialize
			@buffer = []
			@verbose = false
		end
		
		attr :buffer
		attr :verbose
		
		def last
			@buffer.last
		end
		
		def include?(pattern)
			JSON.dump(@buffer).include?(pattern)
		end
		
		def clear
			@buffer.clear
		end
		
		def empty?
			@buffer.empty?
		end
		
		def verbose!(value = true)
			@verbose = value
		end
		
		def verbose?
			@verbose
		end
		
		def call(subject = nil, *arguments, severity: UNKNOWN, **options, &block)
			message = {
				time: ::Time.now.iso8601,
				severity: severity,
				**options,
			}
			
			if subject
				message[:subject] = subject
			end
			
			if arguments.any?
				message[:arguments] = arguments
			end
			
			if annotation = Fiber.current.annotation
				message[:annotation] = annotation
			end
			
			if block_given?
				if block.arity.zero?
					message[:message] = yield
				else
					buffer = StringIO.new
					yield buffer
					message[:message] = buffer.string
				end
			end
			
			@buffer << message
		end
	end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
console-1.24.0 lib/console/capture.rb
console-1.23.7 lib/console/capture.rb
console-1.23.6 lib/console/capture.rb
console-1.23.5 lib/console/capture.rb
console-1.23.4 lib/console/capture.rb
console-1.23.3 lib/console/capture.rb