View Source Carve.Links (Carve v0.3.0)
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, eliminating duplicates, and applying the whitelist filter if provided.
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. It also supports filtering links based on a whitelist.
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: %{})whitelist
: An optional list of allowed link types (default: nil)
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> user = %{id: 1, name: "John Doe", team_id: 2} iex> Carve.Links.get_links_by_data(UserJSON, user, %{}, [:team]) [%{type: :team, id: "abc123", 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. It also supports filtering links based on a whitelist.
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: %{})whitelist
: An optional list of allowed link types (default: nil)
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], %{}, [:team]) [%{type: :team, id: "abc123", data: %{...}}]
Prepares the final result set by flattening, removing nil values, eliminating duplicates, and applying the whitelist filter if provided.
Parameters
result
: A list of link mapswhitelist
: An optional list of allowed link types
Returns
A cleaned, uniquified, and optionally filtered 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, [:team]) [%{type: :team, id: "abc"}]