Class: Prog::Task

Inherits:
Object
  • Object
show all
Defined in:
lib/prog.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(max_count:) ⇒ Task

Returns a new instance of Task.



58
59
60
61
62
# File 'lib/prog.rb', line 58

def initialize(max_count:)
  self.max_count = max_count
  self.current_count = 0
  self.working_length = nil # (Only set by Prog::Pipe)
end

Instance Attribute Details

#current_countObject

Returns the value of attribute current_count.



55
56
57
# File 'lib/prog.rb', line 55

def current_count
  @current_count
end

#max_countObject

Returns the value of attribute max_count.



55
56
57
# File 'lib/prog.rb', line 55

def max_count
  @max_count
end

#pipeObject

Returns the value of attribute pipe.



55
56
57
# File 'lib/prog.rb', line 55

def pipe
  @pipe
end

#working_lengthObject

Returns the value of attribute working_length.



56
57
58
# File 'lib/prog.rb', line 56

def working_length
  @working_length
end

Instance Method Details

#force_finishObject



70
71
72
73
# File 'lib/prog.rb', line 70

def force_finish
  pipe.tasks.delete(self)
  pipe.highest_count = 0 if pipe.tasks.empty?
end

#tick(number_of_ticks = 1) ⇒ Object



64
65
66
67
68
# File 'lib/prog.rb', line 64

def tick(number_of_ticks = 1)
  self.current_count += number_of_ticks

  force_finish if current_count >= max_count
end

#to_sObject



75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/prog.rb', line 75

def to_s
  progress_ratio = current_count / max_count.to_f

  unused_length = working_length
  display_string = +'|' # (not frozen)
  unused_length -= 1

  filled_cells = (unused_length * progress_ratio).floor
  fill_bar = ('=' * filled_cells).ljust(unused_length, ' ')
  display_string.prepend(fill_bar)

  display_string
end