View Source Moar.Opts (Moar v1.5.0)

Extracts keys and values from enumerables, especially from function options.

There are two main functions, each of which takes an opts enumerable as input. get/2 and get/3 each extract one value from the opts. take/2 extracts multiple values from the opts. get/3 and take/2 allow the specification of default values.

Example using get/2 and get/3:

def build_url(path, opts \\ []) do
  %URI{
    path: path,
    host: Moar.Opts.get(opts, :host, "localhost"),
    port: Moar.Opts.get(opts, :port),
    scheme: "https"
  } |> URI.to_string()
end

Examples using take/2:

# example using pattern matching
def build_url(path, opts \ []) do
  %{host: h, port: p} = Moar.Opts.take(opts, [:port, host: "localhost"])
  %URI{path: path, host: h, port: p, scheme: "https"} |> URI.to_string()
end

#example rebinding `opts` to the parsed opts
def build_url(path, opts \ []) do
  opts = Moar.Opts.take(opts, [:port, host: "localhost"])
  %URI{path: path, host: opts.host, port: opts.port, scheme: "https"} |> URI.to_string()
end

Link to this section Summary

Functions

Get the value of key from input, falling back to optional default if the key does not exist.

Get each key in keys from input.

Link to this section Functions

Link to this function

get(input, key, default \\ nil)

View Source
@spec get(Enum.t(), binary() | atom(), any()) :: any()

Get the value of key from input, falling back to optional default if the key does not exist.

iex> [a: 1, b: 2] |> Moar.Opts.get(:a)
1

iex> [a: 1, b: 2] |> Moar.Opts.get(:c)
nil

iex> [a: 1, b: 2] |> Moar.Opts.get(:c, 300)
300
@spec take(Enum.t(), list()) :: map()

Get each key in keys from input.

keys is a list of keys, a keyword list of keys and default values, or a hybrid list/keyword list.

iex> [a: 1, b: 2] |> Moar.Opts.take([:a, :c])
%{a: 1, c: nil}

iex> [a: 1, b: 2] |> Moar.Opts.take([:a, b: 0, c: 3])
%{a: 1, b: 2, c: 3}