exhtml v0.4.0-beta.2 Exhtml.Storage

Storage provides a fetch method used to fetch content from outside source.

Examples:

iex> {:ok, pid} = Exhtml.Storage.start_link []
...> Exhtml.Storage.fetch(pid, :foo)
nil
iex> # fetcher can be replaced at runtime
...> Exhtml.Storage.set_fetcher(pid, fn slug -> "#{slug} is awesome" end)
...> Exhtml.Storage.fetch(pid, :foo)
"foo is awesome"

or you can start a process, passing fetcher:

iex> fetcher = fn slug -> "#{slug} on remote" end
...> {:ok, pid} = Exhtml.Storage.start_link fetcher: fetcher
...> Exhtml.Storage.fetch(pid, :foo)
"foo on remote"

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor

Fetches the content of a slug

Dynamicly set content fetcher. A fetcher can be a function, or a module that can repsond to &fetch/1. If you want to ‘unset’ fetcher, just pass nil as fetcher

Link to this section Types

Link to this type slug()
slug() :: Exhtml.slug()

Link to this section Functions

Link to this function child_spec(arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function fetch(pid, slug)
fetch(GenServer.server(), term()) :: any()

Fetches the content of a slug.

  • pid - the PID or name of the server process
  • slug - the key for the content

Returns nil or the content stored.

Examples:

iex> {:ok, pid} = Exhtml.Storage.start_link [fetcher: fn slug -> "default_content_for_#{slug}" end]
...> Exhtml.Storage.fetch(pid, :foo)
"default_content_for_foo"
Link to this function set_fetcher(pid, f)
set_fetcher(GenServer.server(), (slug() -> any()) | module()) :: :ok

Dynamicly set content fetcher. A fetcher can be a function, or a module that can repsond to &fetch/1. If you want to ‘unset’ fetcher, just pass nil as fetcher.

  • pid - the PID or name of the server process
  • f - function or module to fetch the content by a slug

Returns :ok.

Examples:

use a function as fetcher:

iex> {:ok, pid} = Exhtml.Storage.start_link []
...> Exhtml.Storage.set_fetcher(pid, fn _slug -> :bar end)
:ok

or a module:

iex> defmodule A do
...>   def fetch(slug), do: "Hello, #{slug}~"
...> end
...>
...> {:ok, pid} = Exhtml.Storage.start_link []
...> Exhtml.Storage.set_fetcher(pid, A)
:ok
iex> Exhtml.Storage.fetch(pid, :miao)
"Hello, miao~"