class TaskReport::Task

Constants

TaskOngoing

Attributes

description[R]
id[R]
notes[R]

Public Class Methods

from_existing_tasks(hash) click to toggle source
# 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
new(description:, time: nil, id: nil, notes: nil) click to toggle source
# 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

Public Instance Methods

add_note(note) click to toggle source
# File lib/task_report/task.rb, line 77
def add_note(note)
  @notes << note
end
continue() click to toggle source
# 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() click to toggle source
# File lib/task_report/task.rb, line 47
def duration
  Duration.new(total_time_in_seconds)
end
last_start_time() click to toggle source
# File lib/task_report/task.rb, line 69
def last_start_time
  @time.last[:start]
end
ongoing?() click to toggle source
# File lib/task_report/task.rb, line 73
def ongoing?
  @time.last[:end].nil?
end
stop() click to toggle source
# 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() click to toggle source
# File lib/task_report/task.rb, line 34
def to_h
  {
    id: @id,
    description: @description,
    time: @time,
    notes: @notes
  }
end
to_s() click to toggle source
# File lib/task_report/task.rb, line 43
def to_s
  "Task #{@id}, '#{@description}'"
end
total_time_in_seconds() click to toggle source
# 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