Class: Console::Event::Spawn

Inherits:
Generic
  • Object
show all
Defined in:
lib/console/event/spawn.rb

Overview

Represents a spawn event.

“‘ruby Console.info(self, **Console::Event::Spawn.for(“ls”, “-l”))

event = Console::Event::Spawn.for(“ls”, “-l”) event.status = Process.wait “‘

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Generic

#as_json, #to_json, #to_s

Constructor Details

#initialize(environment, arguments, options) ⇒ Spawn

Returns a new instance of Spawn.



30
31
32
33
34
35
36
37
38
39
# File 'lib/console/event/spawn.rb', line 30

def initialize(environment, arguments, options)
	@environment = environment
	@arguments = arguments
	@options = options
	
	@start_time = Clock.now
	
	@end_time = nil
	@status = nil
end

Class Method Details

.for(*arguments, **options) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/console/event/spawn.rb', line 20

def self.for(*arguments, **options)
	# Extract out the command environment:
	if arguments.first.is_a?(Hash)
		environment = arguments.shift
		self.new(environment, arguments, options)
	else
		self.new(nil, arguments, options)
	end
end

Instance Method Details

#durationObject



41
42
43
44
45
# File 'lib/console/event/spawn.rb', line 41

def duration
	if @end_time
		@end_time - @start_time
	end
end

#emit(*arguments, **options) ⇒ Object



62
63
64
65
# File 'lib/console/event/spawn.rb', line 62

def emit(*arguments, **options)
	options[:severity] ||= :info
	super
end

#status=(status) ⇒ Object



67
68
69
70
# File 'lib/console/event/spawn.rb', line 67

def status=(status)
	@end_time = Time.now
	@status = status
end

#to_hashObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/console/event/spawn.rb', line 47

def to_hash
	Hash.new.tap do |hash|
		hash[:type] = :spawn
		hash[:environment] = @environment if @environment&.any?
		hash[:arguments] = @arguments if @arguments&.any?
		hash[:options] = @options if @options&.any?
		
		hash[:status] = @status.to_i if @status
		
		if duration = self.duration
			hash[:duration] = duration
		end
	end
end