Module: RubyTodo::ExportFileHelpers

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

Overview

Module for handling export-related functionality - Part 3: File and Parameter Handling

Instance Method Summary collapse

Instance Method Details

#default_export_filename(current_time, format, status = "done") ⇒ Object

Update default export filename to reflect the status



593
594
595
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 593

def default_export_filename(current_time, format, status = "done")
  "#{status}_tasks_export_#{current_time.strftime("%Y%m%d")}.#{format}"
end

#export_data_to_file(exported_data, filename, format) ⇒ Object



645
646
647
648
649
650
651
652
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 645

def export_data_to_file(exported_data, filename, format)
  case format
  when "json"
    export_to_json(exported_data, filename)
  when "csv"
    export_to_csv(exported_data, filename)
  end
end

#export_to_csv(exported_data, filename) ⇒ Object



658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 658

def export_to_csv(exported_data, filename)
  require "csv"
  CSV.open(filename, "wb") do |csv|
    # Add headers - Note: "Completed At" is the date when the task was moved to the "done" status
    csv << ["Notebook", "ID", "Title", "Description", "Tags", "Priority", "Created At", "Completed At"]

    # Add data rows
    exported_data["notebooks"].each do |notebook|
      notebook["tasks"].each do |task|
        # Handle tags that might be arrays or comma-separated strings
        tag_value = format_tags_for_csv(task["tags"])

        csv << [
          notebook["name"],
          task["id"] || "N/A",
          task["title"],
          task["description"] || "",
          tag_value,
          task["priority"] || "normal",
          task["created_at"],
          task["updated_at"]
        ]
      end
    end
  end
end

#export_to_json(exported_data, filename) ⇒ Object



654
655
656
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 654

def export_to_json(exported_data, filename)
  File.write(filename, JSON.pretty_generate(exported_data))
end

#extract_custom_filename(prompt, format) ⇒ Object



633
634
635
636
637
638
639
640
641
642
643
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 633

def extract_custom_filename(prompt, format)
  if prompt.match(/to\s+(?:file\s+|filename\s+)?["']?([^"']+)["']?/i)
    filename = ::Regexp.last_match(1).strip
    # Ensure the filename has the correct extension
    unless filename.end_with?(".#{format}")
      filename = "#{filename}.#{format}"
    end
    return filename
  end
  nil
end

#extract_export_parameters(prompt) ⇒ Object



597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 597

def extract_export_parameters(prompt)
  # Default values for an empty prompt
  prompt = prompt.to_s

  # Parse the number of weeks from the prompt
  weeks_regex = /last\s+(\d+)\s+weeks?/i
  weeks = prompt.match(weeks_regex) ? ::Regexp.last_match(1).to_i : 2 # Default to 2 weeks

  # Allow specifying output format - look for explicit CSV mentions
  format = if prompt.match?(/csv/i) || prompt.match?(/to\s+CSV/i) || prompt.match?(/export.*tasks.*to\s+CSV/i)
             "csv"
           else
             "json"
           end

  # Check if a custom filename is specified
  custom_filename = extract_custom_filename(prompt, format)

  # Get current time
  current_time = Time.now

  # Calculate the time from X weeks ago
  weeks_ago = current_time - (weeks * 7 * 24 * 60 * 60)

  # Determine status for the filename
  status = determine_export_status(prompt)

  {
    weeks: weeks,
    format: format,
    filename: custom_filename || default_export_filename(current_time, format, status),
    weeks_ago: weeks_ago,
    status: status
  }
end

#format_tags_for_csv(tags) ⇒ Object



685
686
687
688
689
690
691
692
693
# File 'lib/ruby_todo/commands/ai_assistant.rb', line 685

def format_tags_for_csv(tags)
  if tags.nil?
    ""
  elsif tags.is_a?(Array)
    tags.join(",")
  else
    tags.to_s
  end
end