Module: RubyTodo::ExportCoreHelpers

Included in:
ExportProcessingHelpers
Defined in:
lib/ruby_todo/commands/ai_assistant.rb

Overview

Module for handling export-related functionality - Part 2: Core Export Functions

Instance Method Summary collapse

Instance Method Details

#collect_tasks_by_status(status, weeks_ago = nil) ⇒ Object

Collect tasks with a specific status



542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 542

def collect_tasks_by_status(status, weeks_ago = nil)
  # Collect all notebooks
  notebooks = RubyTodo::Notebook.all

  # Filter for tasks with the specified status
  exported_data = {
    "notebooks" => notebooks.map do |notebook|
      notebook_tasks = notebook.tasks.select do |task|
        if weeks_ago
          task.status == status &&
            task.updated_at &&
            task.updated_at >= weeks_ago
        else
          task.status == status
        end
      end

      {
        "name" => notebook.name,
        "created_at" => notebook.created_at,
        "updated_at" => notebook.updated_at,
        "tasks" => notebook_tasks.map { |task| task_to_hash(task) }
      }
    end
  }

  # Filter out notebooks with no matching tasks
  exported_data["notebooks"].select! { |nb| nb["tasks"].any? }

  exported_data
end

#handle_export_recent_done_tasks(prompt) ⇒ Object

This replaces the old handle_export_recent_done_tasks method



537
538
539
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 537

def handle_export_recent_done_tasks(prompt)
  handle_export_tasks_by_status(prompt, "done")
end

#handle_export_tasks_by_status(prompt, status) ⇒ Object

Handle exporting tasks with a specific status



511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 511

def handle_export_tasks_by_status(prompt, status)
  # Extract export parameters from prompt
  export_params = extract_export_parameters(prompt)

  say "Exporting tasks with status '#{status}'"

  # Collect and filter tasks by status
  exported_data = collect_tasks_by_status(status, export_params[:weeks_ago])

  if exported_data["notebooks"].empty?
    say "No tasks with status '#{status}' found."
    return
  end

  # Count tasks
  total_tasks = exported_data["notebooks"].sum { |nb| nb["tasks"].size }

  # Export data to file
  export_data_to_file(exported_data, export_params[:filename], export_params[:format])

  # Format the success message
  success_msg = "Successfully exported #{total_tasks} '#{status}' tasks to #{export_params[:filename]}."
  say success_msg
end

#task_to_hash(task) ⇒ Object

Helper for task_to_hash in export context



575
576
577
578
579
580
581
582
583
584
585
586
587
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 575

def task_to_hash(task)
  {
    "id" => task.id,
    "title" => task.title,
    "description" => task.description,
    "status" => task.status,
    "priority" => task.priority,
    "tags" => task.tags,
    "due_date" => task.due_date&.iso8601,
    "created_at" => task.created_at&.iso8601,
    "updated_at" => task.updated_at&.iso8601
  }
end