Clarity.Perspective.Lensmaker behaviour (Clarity v0.5.1)

Copy Markdown View Source

Behavior for modules that create and update lenses.

Lensmakers are plugins that can create new lenses or enhance existing ones created by other lensmakers. This allows for a composable system where extensions can add functionality to base lenses.

Example

defmodule MyApp.SecurityLensmaker do
  @behaviour Clarity.Perspective.Lensmaker

  alias Clarity.Graph.Filter
  alias Clarity.Perspective.Lens

  @impl Clarity.Perspective.Lensmaker
  def make_lens do
    %Lens{
      id: "security",
      name: "Security Audit",
      description: "Focus on security-related vertices",
      icon: fn -> ~H"🛡️" end,
      filter: Filter.vertex_type([Auth.Module, Permission.Module])
    }
  end
end

Extension Example

defmodule MyApp.SecurityReportExtension do
  @behaviour Clarity.Perspective.Lensmaker

  @impl Clarity.Perspective.Lensmaker
  def update_lens(%{id: "security"} = lens) do
    # Example: Could modify the lens's content sorter or other properties
    lens
  end

  def update_lens(lens), do: lens
end

Configuration

Register lensmakers in your application configuration:

config :my_app, :clarity_perspective_lensmakers, [
  MyApp.SecurityLensmaker,
  MyApp.SecurityReportExtension
]

Two-Phase Lens Creation

  1. Creation Phase: Calls make_lens/0 on lensmakers that implement it
  2. Enhancement Phase: Calls update_lens/1 on all lensmakers for each lens

This allows extensions to enhance base lenses created by other modules.

Summary

Callbacks

Creates a new lens.

Updates an existing lens, potentially enhancing it with additional functionality.

Types

result(type)

@type result(type) :: {:ok, type} | {:error, term()}

t()

@type t() :: module()

Callbacks

make_lens()

(optional)
@callback make_lens() :: Clarity.Perspective.Lens.t()

Creates a new lens.

update_lens(lens)

(optional)
@callback update_lens(lens :: Clarity.Perspective.Lens.t()) ::
  Clarity.Perspective.Lens.t()

Updates an existing lens, potentially enhancing it with additional functionality.

Extensions typically check the lens ID and only modify lenses they are designed to enhance.