ecto_list v0.1.1 EctoList 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 items_order list.

Link to this section Functions

Link to this function

complete_items_order(items, 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]}]

items_order = [5, 3, 1]

complete_items_order(all_items, 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, 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]}]

items_order = [5, 3, 1]

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

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

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

If ids are missing in items_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]}]

items_order = [5, 3, 1]

ordered_items_list(all_items, 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]}]