Module: TableUtils::Progress

Defined in:
lib/table_utils/progress.rb

Constant Summary collapse

DefaultOptions =
{
  format: "%a |%b %c/%C =>%i| %E",
  throttle_rate: 0.1,
}

Class Method Summary collapse

Class Method Details

.bar(options = {}) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/table_utils/progress.rb', line 10

def self.bar options = {}
  bar = ProgressBar.create DefaultOptions.merge options
  bar.format("%a: |%i| %c") if bar.total == nil
  if block_given?
    begin
      yield bar
    ensure
      if bar.total
        bar.finish unless bar.finished?
      end
    end
  else
    bar
  end
end

.over(enum, options = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/table_utils/progress.rb', line 26

def self.over enum, options = {}
  options = options.dup

  unless options.include? :total
    options[:total] = enum.count
  end

  Progress.bar options do |bar|
    enum.each do |i|
      bar.increment
      yield i, bar
    end
  end
end