Algae v0.12.1 Algae.Reader

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}

Summary

Functions

Alias for run

Simply invoke the reader function on the contained environment

Types

t :: %Algae.Reader{env: any, reader: (... -> any)}

Functions

read(reader)

Specs

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"
run(map)

Specs

run(t) :: any

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"
...>     }
...>   }
...> :uri |> Algae.Reader.run(config).()
"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"