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.
12 13 14 15 |
# File 'lib/console/capture.rb', line 12 def initialize @records = [] @verbose = false end |
Instance Attribute Details
#records ⇒ Object (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 |
#verbose ⇒ Object (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, **, &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
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
50 51 52 |
# File 'lib/console/capture.rb', line 50 def empty? @records.empty? end |
#first ⇒ Object
38 39 40 |
# File 'lib/console/capture.rb', line 38 def first @records.first end |
#include?(pattern) ⇒ 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 |
#last ⇒ Object
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
58 59 60 |
# File 'lib/console/capture.rb', line 58 def verbose? @verbose end |