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.



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

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

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



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

def buffer
  @buffer
end

#verboseObject (readonly)

Returns the value of attribute verbose.



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

def verbose
  @verbose
end

Instance Method Details

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



43
44
45
46
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 43

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 annotation = Fiber.current.annotation
    message[:annotation] = annotation
  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



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

def clear
  @buffer.clear
end

#empty?Boolean

Returns:

  • (Boolean)


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

def empty?
  @buffer.empty?
end

#include?(pattern) ⇒ Boolean

Returns:

  • (Boolean)


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

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

#lastObject



19
20
21
# File 'lib/console/capture.rb', line 19

def last
  @buffer.last
end

#verbose!(value = true) ⇒ Object



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

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

#verbose?Boolean

Returns:

  • (Boolean)


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

def verbose?
  @verbose
end