Class: Helper::AsyncCommand

Inherits:
Object
  • Object
show all
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

Constructor Details

#initialize(command) ⇒ AsyncCommand

Returns a new instance of AsyncCommand.



155
156
157
158
159
160
161
162
# File 'lib/helper.rb', line 155

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



146
147
148
149
150
151
152
153
# File 'lib/helper.rb', line 146

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

#responseObject



168
169
170
# File 'lib/helper.rb', line 168

def response
  @command_response
end

#running?Boolean

Returns:

  • (Boolean)


164
165
166
# File 'lib/helper.rb', line 164

def running?
  @command_running
end