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.



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

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

Instance Attribute Details

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

Returns the value of attribute records.



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

def records
  @records
end

#verboseObject (readonly)

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

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



61
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
# File 'lib/console/capture.rb', line 61

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



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

def clear
	@records.clear
end

#each(&block) ⇒ Object



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

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

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
	@records.empty?
end

#firstObject



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

def first
	@records.first
end

#include?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#lastObject



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

def last
	@records.last
end

#verbose!(value = true) ⇒ Object



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

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

#verbose?Boolean

Returns:

  • (Boolean)


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

def verbose?
	@verbose
end