Class: TTY::ProgressBar::Pipeline Private

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/tty/progressbar/pipeline.rb

Overview

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Used by TTY::ProgressBar to decorate format string

Instance Method Summary collapse

Constructor Details

#initialize(formatters = []) ⇒ Pipeline

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Create formatting pipeline


12
13
14
15
# File 'lib/tty/progressbar/pipeline.rb', line 12

def initialize(formatters = [])
  @formatters = formatters
  freeze
end

Instance Method Details

#decorate(progress, tokenized) ⇒ nil

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Decorate the tokenized string with actual values

Returns:

  • (nil)

32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tty/progressbar/pipeline.rb', line 32

def decorate(progress, tokenized)
  base = tokenized.dup
  formatters.inject(base) do |formatted, formatter|
    instance = formatter.call(progress)
    if instance.respond_to?(:matches?) && instance.matches?(formatted)
      instance.format(formatted)
    else
      formatted
    end
  end
end

#each(&block) ⇒ Object

Iterate over formatters


47
48
49
# File 'lib/tty/progressbar/pipeline.rb', line 47

def each(&block)
  formatters.each(&block)
end

#use(formatter) ⇒ Object

Add a new formatter

Examples:

use(TTY::ProgressBar::TotalFormatter)

23
24
25
# File 'lib/tty/progressbar/pipeline.rb', line 23

def use(formatter)
  formatters << proc { |progress| formatter.new(progress) }
end