ecto_playlist v0.0.1 EctoPlaylist View Source

Implements conveniences to handle the items order of a list.

Link to this section Summary

Functions

Returns the list of ids composed of the current list order + all the missings ids ordered by insertion date.

Same as missing_ids_list/2 but returns all ids ordered by insertion date.

Returns the list of missing ids ordered by insertion date.

Returns the list of items ordered according to the order list.

Link to this section Functions

Link to this function

complete_order(items, order) View Source

Returns the list of ids composed of the current list order + all the missings ids ordered by insertion date.

Examples

all_items = [%{id: 1, title: "Item 1", inserted_at: ~N[2019-07-16 16:03:15]},
    %{id: 2, title: "Item 2", inserted_at: ~N[2019-07-16 16:04:15]},
    %{id: 3, title: "Item 3", inserted_at: ~N[2019-07-16 16:05:15]},
    %{id: 4, title: "Item 4", inserted_at: ~N[2019-07-16 16:06:15]},
    %{id: 5, title: "Item 5", inserted_at: ~N[2019-07-16 16:07:15]}]

order = [5, 3, 1]

complete_order(all_items, order)
# [5, 3, 1, 2, 4]
Link to this function

missing_ids_list(all_items) View Source

Same as missing_ids_list/2 but returns all ids ordered by insertion date.

Link to this function

missing_ids_list(all_items, order) View Source

Returns the list of missing ids ordered by insertion date.

Examples

all_items = [%{id: 1, title: "Item 1", inserted_at: ~N[2019-07-16 16:03:15]},
    %{id: 2, title: "Item 2", inserted_at: ~N[2019-07-16 16:04:15]},
    %{id: 3, title: "Item 3", inserted_at: ~N[2019-07-16 16:05:15]},
    %{id: 4, title: "Item 4", inserted_at: ~N[2019-07-16 16:06:15]},
    %{id: 5, title: "Item 5", inserted_at: ~N[2019-07-16 16:07:15]}]

order = [5, 3, 1]

missing_ids_list(all_items, order)
# [2, 4]
Link to this function

ordered_items_list(items, order \\ []) View Source

Returns the list of items ordered according to the order list.

If ids are missing in order, the items will be ordered according to their inserted date.

Examples

all_items = [%{id: 1, title: "Item 1", inserted_at: ~N[2019-07-16 16:03:15]},
    %{id: 2, title: "Item 2", inserted_at: ~N[2019-07-16 16:04:15]},
    %{id: 3, title: "Item 3", inserted_at: ~N[2019-07-16 16:05:15]},
    %{id: 4, title: "Item 4", inserted_at: ~N[2019-07-16 16:06:15]},
    %{id: 5, title: "Item 5", inserted_at: ~N[2019-07-16 16:07:15]}]

order = [5, 3, 1]

ordered_items_list(all_items, order)
# [%{id: 5, title: "Item 5", inserted_at: ~N[2019-07-16 16:07:15]},
#    %{id: 3, title: "Item 3", inserted_at: ~N[2019-07-16 16:05:15]},
#    %{id: 1, title: "Item 1", inserted_at: ~N[2019-07-16 16:03:15]},
#    %{id: 2, title: "Item 2", inserted_at: ~N[2019-07-16 16:04:15]},
#    %{id: 4, title: "Item 4", inserted_at: ~N[2019-07-16 16:06:15]}]