Class: Console::Capture

Inherits:
Object
  • Object
show all
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.



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

def initialize
  @buffer = []
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



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

def buffer
  @buffer
end

Instance Method Details

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



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/console/capture.rb', line 47

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 block_given?
    if block.arity.zero?
      message[:message] = yield
    else
      buffer = StringIO.new
      yield buffer
      message[:message] = buffer.string
    end
  end
  
  @buffer << message
end

#clearObject



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

def clear
  @buffer.clear
end

#include?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


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

def include?(pattern)
  JSON.dump(@buffer).include?(pattern)
end

#lastObject



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

def last
  @buffer.last
end

#verbose!(value = true) ⇒ Object



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

def verbose!(value = true)
end