Class: TaskReport::Report

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

Constant Summary collapse

TaskAlreadyTracked =
Class.new StandardError
TaskAlreadyOngoing =
Class.new StandardError
TaskDNE =
Class.new StandardError
MultipleOngoingTasks =
Class.new StandardError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#gist_idObject (readonly)

Returns the value of attribute gist_id.



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

def gist_id
  @gist_id
end

Class Method Details

.create(new_task_description:) ⇒ Object



19
20
21
22
23
24
# File 'lib/task_report/report.rb', line 19

def create(new_task_description:)
  Report.new(
    description: gist_description,
    json_file_name: json_file_name
  )
end

.create_from_gist(gist) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/task_report/report.rb', line 26

def create_from_gist(gist)
  description = gist['description'] || gist_description
  raw_url = gist['files'][json_file_name(description)]['raw_url']

  Report.new(
    description: description,
    json_file_name: json_file_name,
    gist_id: gist['id'],
    gist_html_url: gist['html_url'],
    existing_json_content: Gist.file_content(raw_url)
  )
end

.gist_description(time = Time.now) ⇒ Object



11
12
13
# File 'lib/task_report/report.rb', line 11

def gist_description(time = Time.now)
  "#{User.name}_report_#{time.strftime('%Y-%m-%d')}"
end

.json_file_name(description = nil) ⇒ Object



15
16
17
# File 'lib/task_report/report.rb', line 15

def json_file_name(description = nil)
  "#{description || gist_description}.json"
end

Instance Method Details

#add_note(task_id, note) ⇒ Object

Raises:



155
156
157
158
159
160
161
# File 'lib/task_report/report.rb', line 155

def add_note(task_id, note)
  task = find_task_by_id(task_id)
  raise TaskDNE if task.nil?

  task.add_note(note)
  puts "Note added to #{task.to_s}"
end

#continue(task_id) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/task_report/report.rb', line 54

def continue(task_id)
  ensure_no_tasks_ongoing!

  task =
    if task_id.nil?
      find_last_task_to_be_worked_on
    else
      find_task_by_id(task_id) || find_task_by_description(task_id)
    end

  task.continue
end

#delete(task_id) ⇒ Object

Raises:



67
68
69
70
71
72
73
# File 'lib/task_report/report.rb', line 67

def delete(task_id)
  task = find_task_by_id(task_id) || find_task_by_description(task_id)
  raise TaskDNE if task.nil?

  puts "Deleting #{task.to_s}."
  @tasks.delete_if { |t| t.id == task.id }
end

#delete_allObject



75
76
77
78
# File 'lib/task_report/report.rb', line 75

def delete_all
  puts "Deleting all tasks for today."
  @tasks = []
end

#gist_summaryObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/task_report/report.rb', line 127

def gist_summary
  if @tasks.empty?
    puts 'There are no tasks reported for today.'
    return
  end

  puts 'Creating a gist summary.'

  Gist.edit(@gist_id,
    description: @description, # do we actually need this? Seems odd...
    files: {
      'summary.md' => {
        content: gist_summary_content
      }
    }
  )

  puts "#{@gist_html_url}#file-summary-md"
end

#gist_summary_contentObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/task_report/report.rb', line 167

def gist_summary_content
  lines = ["## #{User.name} Task Report #{@date.strftime('%Y-%m-%d')}", '']

  @tasks.each do |task|
    lines << "- '#{task.description}'"
    lines << "  - #{task.duration.to_s}"

    task.notes.each do |note|
      lines << "  - #{note}"
    end
  end

  lines << ''
  lines << "#### Total time tracked: #{total_duration.to_s}"

  lines.join("\n")
end


93
94
95
96
97
98
99
100
101
102
103
# File 'lib/task_report/report.rb', line 93

def print_current_task
  ensure_only_one_ongoing_task!

  task = @tasks.find(&:ongoing?)

  if task.nil?
    puts "There is no task ongoing."
  else
    puts "Curent task: #{task.to_s}"
  end
end


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/task_report/report.rb', line 105

def print_summary
  if @tasks.empty?
    puts 'There are no tasks reported for today.'
    return
  end

  puts "#{User.name} Task Report #{@date.strftime('%Y-%m-%d')}"

  @tasks.each do |task|
    puts "'#{task.description}'"
    puts "  - #{task.duration.to_s}"

    task.notes.each do |note|
      puts "  - #{note}"
    end

    puts "\n"
  end

  puts "Total time tracked: #{total_duration.to_s}\n\n"
end


80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/task_report/report.rb', line 80

def print_tasks
  if @tasks.empty?
    puts 'There are no tasks reported for today.'
    return
  end

  puts "Tasks:"

  @tasks.each do |task|
    puts "- #{task.to_s}"
  end
end

#save_to_gist!Object



147
148
149
150
151
152
153
# File 'lib/task_report/report.rb', line 147

def save_to_gist!
  if @gist_id
    edit_existing_data_gist!
  else
    save_new_data_gist!
  end
end

#start_task(new_task_description) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/task_report/report.rb', line 40

def start_task(new_task_description)
  if @tasks.any? { |t| t.description == new_task_description }
    raise TaskAlreadyTracked
  end

  task = Task.new(description: new_task_description)
  puts "Starting #{task.to_s}."
  @tasks << task
end

#stop_all_tasksObject



50
51
52
# File 'lib/task_report/report.rb', line 50

def stop_all_tasks
  @tasks.each(&:stop)
end

#totalObject



163
164
165
# File 'lib/task_report/report.rb', line 163

def total
  puts total_duration.to_s
end