Class: ProgressMonitor::Task
- Inherits:
-
Object
- Object
- ProgressMonitor::Task
- Includes:
- Observable
- Defined in:
- lib/progress_monitor/task.rb,
lib/progress_monitor/task/percentage_calculation/status.rb,
lib/progress_monitor/task/percentage_calculation/io_position.rb,
lib/progress_monitor/task/percentage_calculation/average_completion_of_subtasks.rb
Defined Under Namespace
Modules: PercentageCalculation
Constant Summary collapse
- PERCENTAGE_CALCULATORS =
[ PercentageCalculation::Status, PercentageCalculation::IoPosition, PercentageCalculation::AverageCompletionOfSubtasks, ]
Instance Attribute Summary collapse
-
#finished_at ⇒ Object
readonly
Returns the value of attribute finished_at.
-
#io ⇒ Object
Returns the value of attribute io.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#started_at ⇒ Object
readonly
Returns the value of attribute started_at.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#subtasks ⇒ Object
readonly
Returns the value of attribute subtasks.
Instance Method Summary collapse
- #add_and_run_subtasks(items, name_fn, &run_fn) ⇒ Object
- #add_subtask(name) ⇒ Object
- #add_subtasks(*names) ⇒ Object
- #completion_percent ⇒ Object
- #duration ⇒ Object
- #finish ⇒ Object
-
#initialize(name, parent = nil) ⇒ Task
constructor
A new instance of Task.
- #start ⇒ Object
Constructor Details
#initialize(name, parent = nil) ⇒ Task
Returns a new instance of Task.
18 19 20 21 22 23 |
# File 'lib/progress_monitor/task.rb', line 18 def initialize(name, parent=nil) @name = name @parent = parent @subtasks = [] @status = :new end |
Instance Attribute Details
#finished_at ⇒ Object (readonly)
Returns the value of attribute finished_at.
9 10 11 |
# File 'lib/progress_monitor/task.rb', line 9 def finished_at @finished_at end |
#io ⇒ Object
Returns the value of attribute io.
10 11 12 |
# File 'lib/progress_monitor/task.rb', line 10 def io @io end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'lib/progress_monitor/task.rb', line 9 def name @name end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
9 10 11 |
# File 'lib/progress_monitor/task.rb', line 9 def parent @parent end |
#started_at ⇒ Object (readonly)
Returns the value of attribute started_at.
9 10 11 |
# File 'lib/progress_monitor/task.rb', line 9 def started_at @started_at end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
9 10 11 |
# File 'lib/progress_monitor/task.rb', line 9 def status @status end |
#subtasks ⇒ Object (readonly)
Returns the value of attribute subtasks.
9 10 11 |
# File 'lib/progress_monitor/task.rb', line 9 def subtasks @subtasks end |
Instance Method Details
#add_and_run_subtasks(items, name_fn, &run_fn) ⇒ Object
57 58 59 60 61 62 63 |
# File 'lib/progress_monitor/task.rb', line 57 def add_and_run_subtasks(items, name_fn, &run_fn) items.map do |item| [item, add_subtask(name_fn.call(item))] end.each do |args| run_fn.call(*args) end end |
#add_subtask(name) ⇒ Object
47 48 49 50 51 |
# File 'lib/progress_monitor/task.rb', line 47 def add_subtask(name) Task.new(name, self).tap do |task| @subtasks << task end end |
#add_subtasks(*names) ⇒ Object
53 54 55 |
# File 'lib/progress_monitor/task.rb', line 53 def add_subtasks(*names) names.map &method(:add_subtask) end |
#completion_percent ⇒ Object
65 66 67 68 69 70 71 72 73 74 |
# File 'lib/progress_monitor/task.rb', line 65 def completion_percent PERCENTAGE_CALCULATORS.each do |calculator| result = calculator.new(self).perform return result if result end :unknown rescue => e puts e.inspect :unknown end |
#duration ⇒ Object
43 44 45 |
# File 'lib/progress_monitor/task.rb', line 43 def duration finished_at - started_at end |
#finish ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/progress_monitor/task.rb', line 34 def finish return unless status == :started parent.finish_if_subtasks_finished @status = :finished @finished_at = Time.now notify_status end |
#start ⇒ Object
25 26 27 28 29 30 31 32 |
# File 'lib/progress_monitor/task.rb', line 25 def start return unless status == :new parent.start if parent @status = :started @started_at = Time.now notify_status end |