Sha256: 72016550d0b37cdaddcc6abc4fedfc8eb41ad472b72acbd4dc44b73cb96bb0e3

Contents?: true

Size: 873 Bytes

Versions: 2

Compression:

Stored size: 873 Bytes

Contents

module MultiProgressBar
  # Works just like +Bar+, but displays the total of other bars.
  # TotalBar#inc and TotalBar#set don't work.
  class TotalBar < Bar
    # Create a new TotalBar.  +bars+ is an array of Bar objects, and defaults to
    # all existing bars.
    def initialize(title, bars = MultiProgressBar.bars.dup)
      @bars = bars

      @bars.each do |bar|
        bar.observe do
          update_total
        end
      end

      total_total = @bars.inject(0) { |sum, bar| sum + bar.total }
      super title, total_total
    end

    [:inc, :set].each do |name|
      define_method(name) do
        raise NoMethodError, "NoMethodError: private method `#{name}' called for #{self}"
      end
    end

    private
    def update_total
      total_current = @bars.inject(0) { |sum, bar| sum + bar.current }
      @renderer.set(total_current)
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
multi_progress_bar-0.1.0 lib/multi_progress_bar/total_bar.rb
multi_progress_bar-0.0.0 lib/multi_progress_bar/total_bar.rb