StructAccess v1.1.2 StructAccess View Source

Provides a standard callback implementation for the Access behaviour.

Implements the following callbacks for the struct where this module is used:

To define these callback and include the proper behavior all you have to do is add use StructAccess to the module defining your struct.

Adding

use StructAccess

to a module is equivalent to adding the following to that module:

@behaviour Access

@impl Access
def fetch(struct, key), do: StructAccess.fetch(struct, key)

@impl Access
def get_and_update(struct, key, fun) when is_function(fun, 1) do
  StructAccess.get_and_update(struct, key, fun)
end

@impl Access
def pop(struct, key, default \ nil) do
  StructAccess.pop(struct, key, default)
end

defoverridable Access

This module is simply a shortcut to avoid that boilerplate.

If any of the implementations in StructAccess are not sufficient, they all can be overridden.

Link to this section Summary

Functions

Retrieves the given key from the given struct.

Retrieves the given key from the given struct with a default.

Retrives the given key from the given struct and updates it at the same time.

Pops the given key from the given struct. As struct keys can't be deleted this simply sets the value of the popped key to nil.

Link to this section Functions

Retrieves the given key from the given struct.

Implements the Access.fetch/2 callback.

Link to this function

get(struct, key, default \\ nil)

View Source

Retrieves the given key from the given struct with a default.

Link to this function

get_and_update(struct, key, fun)

View Source

Retrives the given key from the given struct and updates it at the same time.

Implements the Access.get_and_update/3 callback.

Link to this function

pop(struct, key, default \\ nil)

View Source

Pops the given key from the given struct. As struct keys can't be deleted this simply sets the value of the popped key to nil.

Implements the Access.pop/2 callback.