Class: Console::Capture

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/console/capture.rb

Overview

A general sink which captures all events into a buffer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCapture

Returns a new instance of Capture.



12
13
14
15
# File 'lib/console/capture.rb', line 12

def initialize
	@records = []
	@verbose = false
end

Instance Attribute Details

#recordsObject (readonly) Also known as: buffer, to_a

Returns the value of attribute records.



17
18
19
# File 'lib/console/capture.rb', line 17

def records
  @records
end

#verboseObject (readonly)

Returns the value of attribute verbose.



24
25
26
# File 'lib/console/capture.rb', line 24

def verbose
  @verbose
end

Instance Method Details

#call(subject = nil, *arguments, severity: UNKNOWN, event: nil, **options, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/console/capture.rb', line 62

def call(subject = nil, *arguments, severity: UNKNOWN, event: nil, **options, &block)
	record = {
		time: ::Time.now.iso8601,
		severity: severity,
		**options,
	}
	
	if subject
		record[:subject] = subject
	end
	
	if event
		record[:event] = event.to_hash
	end
	
	if arguments.any?
		record[:arguments] = arguments
	end
	
	if annotation = Fiber.current.annotation
		record[:annotation] = annotation
	end
	
	if block_given?
		if block.arity.zero?
			record[:message] = yield
		else
			buffer = StringIO.new
			yield buffer
			record[:message] = buffer.string
		end
	else
		record[:message] = arguments.join(" ")
	end
	
	@records << record
end

#clearObject



46
47
48
# File 'lib/console/capture.rb', line 46

def clear
	@records.clear
end

#each(&block) ⇒ Object



32
33
34
# File 'lib/console/capture.rb', line 32

def each(&block)
	@records.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/console/capture.rb', line 50

def empty?
	@records.empty?
end

#firstObject



38
39
40
# File 'lib/console/capture.rb', line 38

def first
	@records.first
end

#include?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/console/capture.rb', line 26

def include?(pattern)
	@records.any? do |record|
		record[:subject].to_s&.match?(pattern) or record[:message].to_s&.match?(pattern)
	end
end

#lastObject



42
43
44
# File 'lib/console/capture.rb', line 42

def last
	@records.last
end

#verbose!(value = true) ⇒ Object



54
55
56
# File 'lib/console/capture.rb', line 54

def verbose!(value = true)
	@verbose = value
end

#verbose?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/console/capture.rb', line 58

def verbose?
	@verbose
end