Module: RubyTodo::ImportExport

Includes:
CsvExport
Included in:
CLI
Defined in:
lib/ruby_todo/concerns/import_export.rb

Instance Method Summary collapse

Instance Method Details

#export_all_notebooks(notebooks) ⇒ Object



62
63
64
65
66
# File 'lib/ruby_todo/concerns/import_export.rb', line 62

def export_all_notebooks(notebooks)
  {
    "notebooks" => notebooks.map { |notebook| export_notebook(notebook) }
  }
end

#export_notebook(notebook) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/ruby_todo/concerns/import_export.rb', line 53

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

#export_to_csv(data, filename) ⇒ Object



85
86
87
88
89
90
91
92
93
# File 'lib/ruby_todo/concerns/import_export.rb', line 85

def export_to_csv(data, filename)
  CSV.open(filename, "wb") do |csv|
    if data["notebooks"]
      export_multiple_notebooks_to_csv(data, csv)
    else
      export_single_notebook_to_csv(data, csv)
    end
  end
end

#export_to_json(data, filename) ⇒ Object



81
82
83
# File 'lib/ruby_todo/concerns/import_export.rb', line 81

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

#import_all_notebooks(data) ⇒ Object



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

def import_all_notebooks(data)
  results = { notebooks: 0, tasks: 0 }

  data["notebooks"].each do |notebook_data|
    notebook_name = notebook_data["name"]
    notebook = Notebook.find_by(name: notebook_name)

    unless notebook
      notebook = Notebook.create(name: notebook_name)
      results[:notebooks] += 1 if notebook.persisted?
    end

    if notebook.persisted?
      tasks_count = import_tasks(notebook, notebook_data["tasks"])
      results[:tasks] += tasks_count
    end
  end

  results
end

#import_tasks(notebook, tasks_data) ⇒ Object



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ruby_todo/concerns/import_export.rb', line 95

def import_tasks(notebook, tasks_data)
  count = 0

  tasks_data.each do |task_data|
    due_date = Time.parse(task_data["due_date"]) if task_data["due_date"]

    task = Task.create(
      notebook: notebook,
      title: task_data["title"],
      description: task_data["description"],
      status: task_data["status"] || "todo",
      priority: task_data["priority"],
      tags: task_data["tags"],
      due_date: due_date
    )

    count += 1 if task.persisted?
  end

  count
end

#task_to_hash(task) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ruby_todo/concerns/import_export.rb', line 68

def task_to_hash(task)
  {
    "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