Class: Minder::TaskRecorder
- Inherits:
-
Object
- Object
- Minder::TaskRecorder
- Defined in:
- lib/minder/task_recorder.rb
Instance Attribute Summary collapse
-
#database ⇒ Object
readonly
Returns the value of attribute database.
-
#lines ⇒ Object
Returns the value of attribute lines.
-
#search_results ⇒ Object
readonly
Returns the value of attribute search_results.
-
#tasks ⇒ Object
readonly
Returns the value of attribute tasks.
Instance Method Summary collapse
- #add_task(task) ⇒ Object
- #add_to_done_file(text) ⇒ Object
- #complete_task ⇒ Object
- #delete_task ⇒ Object
- #fetch_filtered_tasks ⇒ Object
- #filter(text) ⇒ Object
-
#initialize(database:) ⇒ TaskRecorder
constructor
A new instance of TaskRecorder.
- #next_search ⇒ Object
- #previous_search ⇒ Object
- #reload ⇒ Object
- #search(text) ⇒ Object
- #select_first_task ⇒ Object
- #select_last_task ⇒ Object
- #select_next_task ⇒ Object
- #select_previous_task ⇒ Object
- #select_search_result(search_index = 0) ⇒ Object
- #selected_task ⇒ Object
- #selected_task_index ⇒ Object
- #start_task ⇒ Object
- #started_task ⇒ Object
- #tasks? ⇒ Boolean
- #unfiltered_tasks ⇒ Object
- #unstart_task ⇒ Object
- #write_file(path) ⇒ Object
- #write_file_with_backup ⇒ Object
Constructor Details
#initialize(database:) ⇒ TaskRecorder
Returns a new instance of TaskRecorder.
12 13 14 15 16 17 18 19 |
# File 'lib/minder/task_recorder.rb', line 12 def initialize(database:) @database = database @selected_task_index = 0 @selected_search_result = 0 @search_results = [] @filter = '' reload end |
Instance Attribute Details
#database ⇒ Object (readonly)
Returns the value of attribute database.
8 9 10 |
# File 'lib/minder/task_recorder.rb', line 8 def database @database end |
#lines ⇒ Object
Returns the value of attribute lines.
6 7 8 |
# File 'lib/minder/task_recorder.rb', line 6 def lines @lines end |
#search_results ⇒ Object (readonly)
Returns the value of attribute search_results.
8 9 10 |
# File 'lib/minder/task_recorder.rb', line 8 def search_results @search_results end |
#tasks ⇒ Object (readonly)
Returns the value of attribute tasks.
8 9 10 |
# File 'lib/minder/task_recorder.rb', line 8 def tasks @tasks end |
Instance Method Details
#add_task(task) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/minder/task_recorder.rb', line 42 def add_task(task) File.open(DOING_FILE, 'a') do |file| file.write("#{task}\n") end database.add_task(task) reload end |
#add_to_done_file(text) ⇒ Object
94 95 96 97 98 |
# File 'lib/minder/task_recorder.rb', line 94 def add_to_done_file(text) File.open(DONE_FILE, 'a') do |file| file.write("[#{Time.now.strftime('%Y-%m-%d %H:%M:%S')}] #{text}\n") end end |
#complete_task ⇒ Object
88 89 90 91 92 |
# File 'lib/minder/task_recorder.rb', line 88 def complete_task task = selected_task delete_task add_to_done_file("Finished: #{task.description}") end |
#delete_task ⇒ Object
74 75 76 77 78 79 80 81 |
# File 'lib/minder/task_recorder.rb', line 74 def delete_task database.delete_task(selected_task) @tasks = nil write_file_with_backup reload select_previous_task end |
#fetch_filtered_tasks ⇒ Object
34 35 36 |
# File 'lib/minder/task_recorder.rb', line 34 def fetch_filtered_tasks database.tasks_filtered_by(@filter) end |
#filter(text) ⇒ Object
21 22 23 24 |
# File 'lib/minder/task_recorder.rb', line 21 def filter(text) @tasks = nil @filter = text end |
#next_search ⇒ Object
156 157 158 159 160 161 162 163 |
# File 'lib/minder/task_recorder.rb', line 156 def next_search @selected_search_result += 1 if @selected_search_result > search_results.length - 1 @selected_search_result = 0 end select_search_result(@selected_search_result) end |
#previous_search ⇒ Object
165 166 167 168 169 170 171 172 173 |
# File 'lib/minder/task_recorder.rb', line 165 def previous_search @selected_search_result -= 1 if @selected_search_result < 0 @selected_search_result = search_results.length - 1 end select_search_result(@selected_search_result) end |
#reload ⇒ Object
83 84 85 86 |
# File 'lib/minder/task_recorder.rb', line 83 def reload self.lines = File.read(DOING_FILE).strip.split("\n") @tasks = nil end |
#search(text) ⇒ Object
141 142 143 144 145 146 |
# File 'lib/minder/task_recorder.rb', line 141 def search(text) @search_results = tasks.select do |task| task.description.downcase.include?(text.downcase) end @selected_search_result = 0 end |
#select_first_task ⇒ Object
137 138 139 |
# File 'lib/minder/task_recorder.rb', line 137 def select_first_task @selected_task_index = 0 end |
#select_last_task ⇒ Object
133 134 135 |
# File 'lib/minder/task_recorder.rb', line 133 def select_last_task @selected_task_index = tasks.length - 1 end |
#select_next_task ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/minder/task_recorder.rb', line 50 def select_next_task if @selected_task_index + 1 <= tasks.length - 1 @selected_task_index += 1 else @selected_task_index = 0 end end |
#select_previous_task ⇒ Object
58 59 60 61 62 63 64 |
# File 'lib/minder/task_recorder.rb', line 58 def select_previous_task if @selected_task_index == 0 @selected_task_index = tasks.length - 1 else @selected_task_index -= 1 end end |
#select_search_result(search_index = 0) ⇒ Object
148 149 150 151 152 153 154 |
# File 'lib/minder/task_recorder.rb', line 148 def select_search_result(search_index = 0) return if search_results.empty? @selected_task_index = tasks.find_index do |task| task.description == search_results[search_index].description end end |
#selected_task ⇒ Object
70 71 72 |
# File 'lib/minder/task_recorder.rb', line 70 def selected_task tasks[selected_task_index] end |
#selected_task_index ⇒ Object
66 67 68 |
# File 'lib/minder/task_recorder.rb', line 66 def selected_task_index @selected_task_index end |
#start_task ⇒ Object
115 116 117 118 119 120 |
# File 'lib/minder/task_recorder.rb', line 115 def start_task database.start_task(selected_task) write_file_with_backup add_to_done_file("Started: #{selected_task.description}") reload end |
#started_task ⇒ Object
129 130 131 |
# File 'lib/minder/task_recorder.rb', line 129 def started_task tasks.find(&:started?) end |
#tasks? ⇒ Boolean
38 39 40 |
# File 'lib/minder/task_recorder.rb', line 38 def tasks? !tasks.empty? end |
#unfiltered_tasks ⇒ Object
30 31 32 |
# File 'lib/minder/task_recorder.rb', line 30 def unfiltered_tasks database.tasks end |
#unstart_task ⇒ Object
122 123 124 125 126 127 |
# File 'lib/minder/task_recorder.rb', line 122 def unstart_task database.unstart_task(selected_task) write_file_with_backup add_to_done_file("Un-started: #{selected_task.description}") reload end |
#write_file(path) ⇒ Object
105 106 107 108 109 110 111 112 113 |
# File 'lib/minder/task_recorder.rb', line 105 def write_file(path) File.open(path, 'w') do |file| unfiltered_tasks.each do |task| line = task.to_s line = "* #{line}" if task.started? file.write("#{line}\n") end end end |
#write_file_with_backup ⇒ Object
100 101 102 103 |
# File 'lib/minder/task_recorder.rb', line 100 def write_file_with_backup FileUtils.cp(DOING_FILE, DOING_FILE + '.old') write_file(DOING_FILE) end |