View Source Pathex.Accessibility (Pathex v2.6.1)
Helpers to create paths from Access.t() and lists
Note: This module is separate because functions presented in this module are suboptimal. You should try use
Pathex.path/2first, and only if its not applicable to you use-case, you should use functions from this module
Summary
Functions
Creates path-closure from Access.t().
Creates path from list of items. The list of items should not be known
at runtime, therefore some optimizations of paths are not possible. Use this
function only when Pathex.path/2 is not applicable
Creates a lens from record macro. All arguments of this macro must be compile time atoms
Creates a lens for specific key in the structure. It behaves the same way as a lens created
with Pathex.path/2 but it works only with specified structure and force_set creates
a structure with default values.
Converts path-closure to Access.t()
Types
@type access() :: [term(), ...]
Functions
Creates path-closure from Access.t().
Note: Paths created using this function do not support force operations yet
Example
iex> import Pathex
iex> p = from_access [:x, :y]
iex> 10 = view!(%{x: [y: 10]}, p)
@spec from_list([any()], Pathex.mod()) :: Pathex.t()
Creates path from list of items. The list of items should not be known
at runtime, therefore some optimizations of paths are not possible. Use this
function only when Pathex.path/2 is not applicable
Example
iex> import Pathex
iex> p = from_list [:x, 1, :y]
iex> 10 = view!(%{x: [1, [y: 10]]}, p)
Creates a lens from record macro. All arguments of this macro must be compile time atoms
Example
iex> require Pathex
iex> defmodule User do
...> import Record; defrecord(:user, name: "", age: nil)
...>
...> def new(name) do
...> name_lens = from_record(User, :user, :name)
...> Pathex.force_set!({}, name_lens, name)
...> end
...> end
iex> {:user, "Joe", nil} = User.new("Joe")
Creates a lens for specific key in the structure. It behaves the same way as a lens created
with Pathex.path/2 but it works only with specified structure and force_set creates
a structure with default values.
Example
iex> require Pathex
iex> defmodule Admin do
...> defstruct name: "", age: nil
...> end
iex> name_lens = from_struct(Admin, :name)
iex> Pathex.force_set!(%{}, name_lens, "Joe")
@spec to_access(Pathex.t()) :: [Access.access_fun(any(), any())]
Converts path-closure to Access.t()
Example
iex> import Pathex
iex> access = to_access path(:x / 0, :map)
iex> 1 = get_in(%{x: %{0 => 1}}, access)