Class: Greeenboii::TodoList
- Inherits:
-
Object
- Object
- Greeenboii::TodoList
- Defined in:
- lib/greeenboii.rb
Instance Method Summary collapse
- #add_task ⇒ Object
- #delete_task ⇒ Object
-
#initialize ⇒ TodoList
constructor
A new instance of TodoList.
- #list_tasks ⇒ Object
- #mark_done ⇒ Object
- #show_menu ⇒ Object
- #update_task ⇒ Object
Constructor Details
#initialize ⇒ TodoList
Returns a new instance of TodoList.
338 339 340 |
# File 'lib/greeenboii.rb', line 338 def initialize @db = setup_database end |
Instance Method Details
#add_task ⇒ Object
355 356 357 358 359 360 361 |
# File 'lib/greeenboii.rb', line 355 def add_task title = CLI::UI.ask("Enter task title:") return if title.empty? @db.execute("INSERT INTO todos (title) VALUES (?)", [title]) puts CLI::UI.fmt "{{green:✓}} Task added successfully!" end |
#delete_task ⇒ Object
387 388 389 390 391 392 393 394 |
# File 'lib/greeenboii.rb', line 387 def delete_task list_tasks id = CLI::UI.ask("Enter task ID to delete:") return if id.empty? @db.execute("DELETE FROM todos WHERE id = ?", [id]) puts CLI::UI.fmt "{{green:✓}} Task deleted!" end |
#list_tasks ⇒ Object
363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
# File 'lib/greeenboii.rb', line 363 def list_tasks tasks = @db.execute("SELECT id, title, completed, created_at FROM todos ORDER BY created_at DESC") if tasks.empty? puts CLI::UI.fmt "{{yellow:⚠}} No tasks found" return end ConsoleTable.define(%w[ID Title Status Created]) do |table| tasks.each do |id, title, completed, created_at| status = completed == 1 ? "{{green:✓}} Done" : "{{red:✗}} Pending" table << [id, title, CLI::UI.fmt(status), created_at] end end end |
#mark_done ⇒ Object
378 379 380 381 382 383 384 385 |
# File 'lib/greeenboii.rb', line 378 def mark_done list_tasks id = CLI::UI.ask("Enter task ID to mark as done:") return if id.empty? @db.execute("UPDATE todos SET completed = 1 WHERE id = ?", [id]) puts CLI::UI.fmt "{{green:✓}} Task marked as done!" end |
#show_menu ⇒ Object
408 409 410 411 412 413 414 415 416 417 418 419 420 |
# File 'lib/greeenboii.rb', line 408 def CLI::UI::Frame.divider("{{v}} Todo List") loop do CLI::UI::Prompt.ask("Todo List Options:") do |handler| handler.option("List Tasks") { list_tasks } handler.option("Add Task") { add_task } handler.option("Mark Done") { mark_done } handler.option("Update Task") { update_task } handler.option("Delete Task") { delete_task } handler.option("Exit") { return } end end end |
#update_task ⇒ Object
396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/greeenboii.rb', line 396 def update_task list_tasks id = CLI::UI.ask("Enter task ID to update:") return if id.empty? title = CLI::UI.ask("Enter new title:") return if title.empty? @db.execute("UPDATE todos SET title = ? WHERE id = ?", [title, id]) puts CLI::UI.fmt "{{green:✓}} Task updated!" end |