Algae v0.12.2 Algae.Reader View Source

A helpful way to store some data, and access it with a set function

reader defaults to returning all of env

Examples

iex> %Algae.Reader{env: "stuff to check"}
%Algae.Reader{env: "stuff to check", reader: &Quark.id/1}

Link to this section Summary

Functions

Alias for run

Simply invoke the reader function on the contained environment

Link to this section Types

Link to this type t() View Source
t() :: %Algae.Reader{env: any, reader: (... -> any)}

Link to this section Functions

Link to this function read(reader) View Source
read(t) :: any

Alias for run

Examples

iex> forty_two = %Algae.Reader{env: 42}
  ...> forty_two |> read
  42

  iex> config =
  ...>   %Algae.Reader{
  ...>     reader: &Map.get/2,
  ...>     env: %{
  ...>       uri:   "https://api.awesomeservice.com",
  ...>       token: "12345"
  ...>     }
  ...>   }
  ...>
  ...> read(config).(:uri)
  "https://api.awesomeservice.com"

  > elapsed_time =
  >  %Algae.Reader.new{
  >    env: %{start_time: 1472717375},
  >    reader:
  >      fn %{start_time: start_time} ->
  >        now = DateTime.now |> DateTime.to_unix
  >        "#{now - start_time}ms"
  >      end
  >    }
  >
  >  read elapsed_time
  "42ms"

Simply invoke the reader function on the contained environment

Note that this auto-curries the reader for convenience (and to make several cases even possible).

Examples

iex> %Algae.Reader{env: 42} |> Algae.Reader.run
42

iex> config =
...>   %Algae.Reader{
...>     reader: &Map.get/2,
...>     env: %{
...>       uri:   "https://api.awesomeservice.com",
...>       token: "12345"
...>     }
...>   }
...>
...> Algae.Reader.run(config).(:uri)
"https://api.awesomeservice.com"

> elapsed_time =
>  %Algae.Reader.new{
>    env: %{start_time: 1472717375},
>    reader:
>      fn %{start_time: start_time} ->
>        now = DateTime.now |> DateTime.to_unix
>        "#{now - start_time}ms"
>      end
>    }
>
>  run(elapsed_time)
"42ms"