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.
111 112 113 114 115 116 117 118 |
# File 'lib/helper.rb', line 111 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
102 103 104 105 106 107 108 109 |
# File 'lib/helper.rb', line 102 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
124 125 126 |
# File 'lib/helper.rb', line 124 def response @command_response end |
#running? ⇒ Boolean
120 121 122 |
# File 'lib/helper.rb', line 120 def running? @command_running end |