Class: Helper::AsyncCommand
- Inherits:
-
Object
- Object
- Helper::AsyncCommand
- Defined in:
- lib/helper.rb
Overview
外部コマンド実行中の待機ループの処理を書けるクラス
response = Helper::AsyncCommand.exec(“処理に時間がかかる外部コマンド”) do
print "*"
end if response.success?
puts "成功しました"
end
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(command) ⇒ AsyncCommand
constructor
A new instance of AsyncCommand.
- #response ⇒ Object
- #running? ⇒ Boolean
Constructor Details
#initialize(command) ⇒ AsyncCommand
Returns a new instance of AsyncCommand.
96 97 98 99 100 101 102 103 |
# File 'lib/helper.rb', line 96 def initialize(command) @command_response = nil @command_running = true Thread.new do @command_response = Open3.capture3(command) @command_running = false end end |
Class Method Details
.exec(command, sleep_time = 0.5, &block) ⇒ Object
87 88 89 90 91 92 93 94 |
# File 'lib/helper.rb', line 87 def self.exec(command, sleep_time = 0.5, &block) async_command = new(command) while async_command.running? block.call sleep(sleep_time) end async_command.response end |
Instance Method Details
#response ⇒ Object
109 110 111 |
# File 'lib/helper.rb', line 109 def response @command_response end |
#running? ⇒ Boolean
105 106 107 |
# File 'lib/helper.rb', line 105 def running? @command_running end |