Skip to content

Commit

Permalink
with_progress メソッドを追加
Browse files Browse the repository at this point in the history
一般のenumeratorを受け取れる。
  • Loading branch information
tkawa committed Apr 18, 2024
1 parent bdf20b1 commit ceeeff9
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions lib/dekiru/data_migration_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,24 @@ def duration
((self.ended_at || Time.current) - self.started_at)
end

def find_each_with_progress(target_scope, options = {})
total = options.delete(:total)
opt = {
format: '%a |%b>>%i| %p%% %t',
}.merge(options).merge(
total: total || target_scope.count,
output: stream
)
@pb = ::ProgressBar.create(opt)
target_scope.find_each do |target|
yield target
def with_progress(enum, options = {})
options = options.dup
options[:total] ||= ((enum.size == Float::INFINITY ? nil : enum.size) rescue nil)
options[:format] ||= options[:total] ? '%a |%b>>%i| %p%% %t' : '%a |%b>>%i| ??%% %t'
options[:output] ||= stream

@pb = ::ProgressBar.create(options)
enum.each do |item|
yield item
@pb.increment
end
@pb.finish
end

def find_each_with_progress(target_scope, options = {}, &block)
with_progress(target_scope.find_each, options, &block)
end

private

def current_transaction_open?
Expand Down

0 comments on commit ceeeff9

Please sign in to comment.