30 lines
548 B
Elixir
30 lines
548 B
Elixir
|
defmodule Todo.CSVImporter do
|
||
|
alias Todo.List
|
||
|
|
||
|
def import(path) do
|
||
|
path
|
||
|
|> File.stream!()
|
||
|
|> Stream.map(&parse_line/1)
|
||
|
|> List.new()
|
||
|
end
|
||
|
|
||
|
defp parse_line(line) do
|
||
|
line
|
||
|
|> String.trim()
|
||
|
|> String.split(",")
|
||
|
|> create_entry()
|
||
|
end
|
||
|
|
||
|
defp create_entry([date, title]) do
|
||
|
final_date = parse_date(date)
|
||
|
|
||
|
%{date: final_date, title: title}
|
||
|
end
|
||
|
|
||
|
defp parse_date(string) do
|
||
|
[year, month, day] = string |> String.split("-") |> Enum.map(&String.to_integer/1)
|
||
|
|
||
|
Date.new!(year, month, day)
|
||
|
end
|
||
|
end
|