Class: TaskReport::Task

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

Constant Summary collapse

TaskOngoing =
Class.new StandardError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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

#descriptionObject (readonly)

Returns the value of attribute description.



8
9
10
# File 'lib/task_report/task.rb', line 8

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



8
9
10
# File 'lib/task_report/task.rb', line 8

def id
  @id
end

#notesObject (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

#continueObject

Raises:



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

#durationObject



47
48
49
# File 'lib/task_report/task.rb', line 47

def duration
  Duration.new(total_time_in_seconds)
end

#last_start_timeObject



69
70
71
# File 'lib/task_report/task.rb', line 69

def last_start_time
  @time.last[:start]
end

#ongoing?Boolean

Returns:

  • (Boolean)


73
74
75
# File 'lib/task_report/task.rb', line 73

def ongoing?
  @time.last[:end].nil?
end

#stopObject



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_hObject



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_sObject



43
44
45
# File 'lib/task_report/task.rb', line 43

def to_s
  "Task #{@id}, '#{@description}'"
end

#total_time_in_secondsObject



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