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.



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

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



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

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



166
167
168
# File 'lib/helper.rb', line 166

def response
  @command_response
end

#running?Boolean

Returns:

  • (Boolean)


162
163
164
# File 'lib/helper.rb', line 162

def running?
  @command_running
end