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
# File 'lib/task_report/report.rb', line 26

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

  Report.new(
    description: gist_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_descriptionObject



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

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

.json_file_nameObject



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

def json_file_name
  "#{gist_description}.json"
end

Instance Method Details

#continue(task_id) ⇒ Object



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

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:



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

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



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

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

#gist_summaryObject



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/task_report/report.rb', line 118

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


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

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


104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/task_report/report.rb', line 104

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}"
  end
end


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

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



138
139
140
141
142
143
144
# File 'lib/task_report/report.rb', line 138

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

#start_task(new_task_description) ⇒ Object



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

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



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

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