View Source Carve.Links (Carve v0.1.1)
Carve.Links provides functionality for retrieving and processing links between different entities.
This module is responsible for:
- Fetching links for entities based on their IDs or data structures
- Handling circular references and preventing infinite loops
- Normalizing and preparing the final result set of links
It works in conjunction with the view modules created using Carve.View to generate a complete set of links for any given entity or set of entities.
Summary
Functions
Retrieves links for a given module and data or list of data.
Retrieves links for a given module and ID or list of IDs.
Prepares the final result set by flattening, removing nil values, and eliminating duplicates.
Functions
Retrieves links for a given module and data or list of data.
This function handles single data structures, lists of data structures, and prevents circular references using a visited map.
Parameters
module
: The module to use for fetching and processing linksdata
: A single data structure or a list of data structuresvisited
: A map to keep track of visited entities (default: %{})
Returns
A list of prepared link maps.
Examples
iex> user = %{id: 1, name: "John Doe", team_id: 2}
iex> Carve.Links.get_links_by_data(UserJSON, user)
[%{type: :team, id: "abc123", data: %{...}}, %{type: :profile, id: "def456", data: %{...}}]
iex> users = [%{id: 1, name: "John"}, %{id: 2, name: "Jane"}]
iex> Carve.Links.get_links_by_data(UserJSON, users)
[%{type: :team, id: "abc123", data: %{...}}, %{type: :profile, id: "def456", data: %{...}}, ...]
Retrieves links for a given module and ID or list of IDs.
This function handles single IDs, lists of IDs, and prevents circular references using a visited map.
Parameters
module
: The module to use for fetching and processing linksid
: A single ID or a list of IDsvisited
: A map to keep track of visited entities (default: %{})
Returns
A list of prepared link maps.
Examples
iex> Carve.Links.get_links_by_id(UserJSON, 1)
[%{type: :team, id: "abc123", data: %{...}}, %{type: :profile, id: "def456", data: %{...}}]
iex> Carve.Links.get_links_by_id(UserJSON, [1, 2, 3])
[%{type: :team, id: "abc123", data: %{...}}, %{type: :profile, id: "def456", data: %{...}}, ...]
Prepares the final result set by flattening, removing nil values, and eliminating duplicates.
Parameters
result
: A list of link maps
Returns
A cleaned and uniquified list of link maps.
Examples
iex> result = [[%{type: :team, id: "abc"}, nil], [%{type: :profile, id: "def"}], %{type: :team, id: "abc"}]
iex> Carve.Links.prepare_result(result)
[%{type: :team, id: "abc"}, %{type: :profile, id: "def"}]