Class: Process::Pipeline::Step

Inherits:
Struct
  • Object
show all
Defined in:
lib/process/pipeline/step.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandObject

Returns the value of attribute command

Returns:

  • (Object)

    the current value of command



26
27
28
# File 'lib/process/pipeline/step.rb', line 26

def command
  @command
end

#tailObject

Returns the value of attribute tail

Returns:

  • (Object)

    the current value of tail



26
27
28
# File 'lib/process/pipeline/step.rb', line 26

def tail
  @tail
end

Instance Method Details

#+(pipeline) ⇒ Object



136
137
138
# File 'lib/process/pipeline/step.rb', line 136

def + pipeline
	pipeline.dup(self)
end

#call(*command) ⇒ Object



35
36
37
# File 'lib/process/pipeline/step.rb', line 35

def call(*command)
	self.class.new(self, command)
end

#dup(tail) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/process/pipeline/step.rb', line 27

def dup(tail)
	if self.tail
		self.class.new(self.tail.dup(tail), self.command)
	else
		self.class.new(tail, self.command)
	end
end

#each_line(*args, &block) ⇒ Object



96
97
98
99
100
101
102
# File 'lib/process/pipeline/step.rb', line 96

def each_line(*args, &block)
	return to_enum(:each_line, *args) unless block_given?
	
	read(**args[0]) do |output|
		output.each_line(&block)
	end
end

#inspectObject



132
133
134
# File 'lib/process/pipeline/step.rb', line 132

def inspect
	self.steps.collect(&:to_s).join(" | ")
end

#read(input: nil, error: $stderr) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/process/pipeline/step.rb', line 76

def read(input: nil, error: $stderr)
	pipe = IO.pipe
	
	Process::Group.wait do |group|
		self.spawn(group, input, pipe[1], error)
	end
	
	pipe[1].close
	
	if block_given?
		yield pipe[0]
	else
		buffer = pipe[0].read
	end
	
	pipe[0].close
	
	return buffer
end

#spawn(group, input, output, error) ⇒ Object



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
73
74
# File 'lib/process/pipeline/step.rb', line 39

def spawn(group, input, output, error)
	if self.command.nil?
		if tail = self.tail
			return tail.call(group, input, output, error)
		else
			return nil
		end
	end
	
	if Hash === self.command.last
		*command, options = self.command
	else
		command = self.command
		options = {}
	end
	
	if self.tail
		pipe = IO.pipe
		
		self.tail.spawn(group, input, pipe[1], error)
		pipe[1].close
		
		options[:in] = pipe[0]
	elsif input
		options[:in] = input
	end
	
	options[:out] = output
	
	# puts "run #{command.inspect} #{options.inspect}"
	group.run(*command, **options) do |exit_status|
		unless exit_status.success?
			raise CommandError.new(self, exit_status)
		end
	end
end

#steps {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



114
115
116
117
118
119
120
121
122
# File 'lib/process/pipeline/step.rb', line 114

def steps(&block)
	return to_enum(:steps) unless block_given?
	
	if self.tail
		self.tail.each(&block)
	end
	
	yield self
end

#to_sObject



124
125
126
127
128
129
130
# File 'lib/process/pipeline/step.rb', line 124

def to_s
	if self.command
		self.command.join(" ")
	else
		nil
	end
end

#write(path, input: nil, error: $stderr) ⇒ Object



104
105
106
107
108
109
110
111
112
# File 'lib/process/pipeline/step.rb', line 104

def write(path, input: nil, error: $stderr)
	File.open(path, "w") do |file|
		Process::Group.wait do |group|
			self.spawn(group, input, file, error)
		end
	end
	
	return self
end