Class: Console::Capture
- Inherits:
-
Object
- Object
- Console::Capture
- Includes:
- Enumerable
- Defined in:
- lib/console/capture.rb
Overview
A general sink which captures all events into a buffer.
Instance Attribute Summary collapse
-
#records ⇒ Object
(also: #buffer, #to_a)
readonly
Returns the value of attribute records.
-
#verbose ⇒ Object
readonly
Returns the value of attribute verbose.
Instance Method Summary collapse
- #call(subject = nil, *arguments, severity: UNKNOWN, event: nil, **options, &block) ⇒ Object
- #clear ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
- #first ⇒ Object
- #include?(pattern) ⇒ Boolean
-
#initialize ⇒ Capture
constructor
A new instance of Capture.
- #last ⇒ Object
- #verbose!(value = true) ⇒ Object
- #verbose? ⇒ Boolean
Constructor Details
#initialize ⇒ Capture
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
#records ⇒ Object (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 |
#verbose ⇒ Object (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, **, &block) record = { time: ::Time.now.iso8601, severity: severity, **, } 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 |
#clear ⇒ Object
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
49 50 51 |
# File 'lib/console/capture.rb', line 49 def empty? @records.empty? end |
#first ⇒ Object
37 38 39 |
# File 'lib/console/capture.rb', line 37 def first @records.first end |
#include?(pattern) ⇒ 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 |
#last ⇒ Object
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
57 58 59 |
# File 'lib/console/capture.rb', line 57 def verbose? @verbose end |