Sha256: 8f9fb3de918bd36d063f3827070e4ed3ee8b0f1b18b20cc1f16ef53b367254a1

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

# encoding: UTF-8

module ProgressReporters

  class StagedProgressReporter < ProgressReporter
    DEFAULT_STAGE = :start

    attr_reader :stage, :on_stage_changed_proc, :on_stage_finished_proc
    protected :on_stage_changed_proc
    protected :on_stage_finished_proc

    def initialize
      super
      @stage = DEFAULT_STAGE
    end

    def set_stage(stage)
      @stage = stage
      self
    end

    def change_stage(new_stage)
      if on_stage_changed_proc
        notify_of_stage_change(new_stage, stage)
      end

      @stage = new_stage
    end

    def report_stage_finished(count, total)
      if on_stage_finished_proc
        notify_of_stage_finish(count, total)
      end

      @last_count = count
    end

    def on_stage_changed(&block)
      @on_stage_changed_proc = block
      self
    end

    def on_stage_finished(&block)
      @on_stage_finished_proc = block
      self
    end

    protected

    # this won't get called if on_progress_proc is nil
    def notify_of_progress(count, total)
      on_progress_proc.call(
        count, total, percentage(count, total), stage
      )
    end

    # this won't get called if on_finished_proc is nil
    def notify_of_stage_finish(count, total)
      on_stage_finished_proc.call(
        count, total, percentage(count, total), stage
      )
    end

    # this won't get called if on_stage_changed_proc is nil
    def notify_of_stage_change(new_stage, old_stage)
      on_stage_changed_proc.call(new_stage, old_stage)
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
progress-reporters-1.0.0 lib/progress-reporters/staged_progress_reporter.rb