Class: TaskReport::Task
- Inherits:
-
Object
- Object
- TaskReport::Task
- Defined in:
- lib/task_report/task.rb
Constant Summary collapse
- TaskOngoing =
Class.new StandardError
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#notes ⇒ Object
readonly
Returns the value of attribute notes.
Class Method Summary collapse
Instance Method Summary collapse
- #add_note(note) ⇒ Object
- #continue ⇒ Object
- #duration ⇒ Object
-
#initialize(description:, time: nil, id: nil, notes: nil) ⇒ Task
constructor
A new instance of Task.
- #last_start_time ⇒ Object
- #ongoing? ⇒ Boolean
- #stop ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
- #total_time_in_seconds ⇒ Object
Constructor Details
#initialize(description:, time: nil, id: nil, notes: nil) ⇒ Task
Returns a new instance of Task.
27 28 29 30 31 32 |
# File 'lib/task_report/task.rb', line 27 def initialize(description:, time: nil, id: nil, notes: nil) @description = description @time = time || [{ start: Time.now, end: nil }] @id = id || SecureRandom.hex(4) @notes = notes || [] end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
8 9 10 |
# File 'lib/task_report/task.rb', line 8 def description @description end |
#id ⇒ Object (readonly)
Returns the value of attribute id.
8 9 10 |
# File 'lib/task_report/task.rb', line 8 def id @id end |
#notes ⇒ Object (readonly)
Returns the value of attribute notes.
8 9 10 |
# File 'lib/task_report/task.rb', line 8 def notes @notes end |
Class Method Details
.from_existing_tasks(hash) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/task_report/task.rb', line 10 def self.from_existing_tasks(hash) time = hash['time'].map do |t| { start: Time.parse(t['start']), end: t['end'].nil? ? nil : Time.parse(t['end']) } end self.new( id: hash['id'], description: hash['description'], time: time, notes: hash['notes'] ) end |
Instance Method Details
#add_note(note) ⇒ Object
77 78 79 |
# File 'lib/task_report/task.rb', line 77 def add_note(note) @notes << note end |
#continue ⇒ Object
63 64 65 66 67 |
# File 'lib/task_report/task.rb', line 63 def continue raise TaskOngoing if @time.last[:end].nil? puts "Continuing #{self.to_s}" @time << { start: Time.now, end: nil } end |
#duration ⇒ Object
47 48 49 |
# File 'lib/task_report/task.rb', line 47 def duration Duration.new(total_time_in_seconds) end |
#last_start_time ⇒ Object
69 70 71 |
# File 'lib/task_report/task.rb', line 69 def last_start_time @time.last[:start] end |
#ongoing? ⇒ Boolean
73 74 75 |
# File 'lib/task_report/task.rb', line 73 def ongoing? @time.last[:end].nil? end |
#stop ⇒ Object
57 58 59 60 61 |
# File 'lib/task_report/task.rb', line 57 def stop return unless @time.last[:end].nil? puts "Stopping #{self.to_s}" @time.last[:end] = Time.now end |
#to_h ⇒ Object
34 35 36 37 38 39 40 41 |
# File 'lib/task_report/task.rb', line 34 def to_h { id: @id, description: @description, time: @time, notes: @notes } end |
#to_s ⇒ Object
43 44 45 |
# File 'lib/task_report/task.rb', line 43 def to_s "Task #{@id}, '#{@description}'" end |
#total_time_in_seconds ⇒ Object
51 52 53 54 55 |
# File 'lib/task_report/task.rb', line 51 def total_time_in_seconds @time.inject(0) do |sum, time| sum + ( (time[:end] || Time.now) - time[:start] ) end end |