Class: Console::Output::Serialized

Inherits:
Object
  • Object
show all
Defined in:
lib/console/output/serialized.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, format: Format.default, **options) ⇒ Serialized

Returns a new instance of Serialized.



13
14
15
16
# File 'lib/console/output/serialized.rb', line 13

def initialize(io, format: Format.default, **options)
	@io = io
	@format = format
end

Instance Attribute Details

#formatObject (readonly)

Returns the value of attribute format.



24
25
26
# File 'lib/console/output/serialized.rb', line 24

def format
  @format
end

#ioObject (readonly)

Returns the value of attribute io.



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

def io
  @io
end

Instance Method Details

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



30
31
32
33
34
35
36
37
38
39
40
41
42
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
# File 'lib/console/output/serialized.rb', line 30

def call(subject = nil, *arguments, severity: UNKNOWN, **options, &block)
	record = {
		time: Time.now.iso8601,
		severity: severity,
		oid: subject.object_id,
		pid: Process.pid,
	}
	
	# We want to log just a brief subject:
	if subject.is_a?(String)
		record[:subject] = subject
	elsif subject.is_a?(Module)
		record[:subject] = subject.name
	else
		record[:subject] = subject.class.name
	end
	
	if annotation = Fiber.current.annotation
		record[:annotation] = annotation
	end
	
	message = arguments
	
	if block_given?
		if block.arity.zero?
			message << yield
		else
			buffer = StringIO.new
			yield buffer
			message << buffer.string
		end
	end
	
	if message.size == 1
		record[:message] = message.first
	elsif message.any?
		record[:message] = message
	end
	
	record.update(options)
	
	@io.puts(self.dump(record))
end

#dump(record) ⇒ Object



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

def dump(record)
	@format.dump(record)
end

#last_outputObject

This a final output that then writes to an IO object.



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

def last_output
	self
end