Belt v0.5.1 Belt.Provider.Helpers View Source

Provides functions shared across providers.

Link to this section Summary

Functions

Ensures path is included in a directory

Creates a Belt.FileInfo struct from a local file

Increments the key part of a path

Splits a key into its base name and extension

Link to this section Functions

Link to this function ensure_included(path, directory) View Source
ensure_included(Path.t(), Path.t()) :: {:ok, Path.t()} | {:error, term()}

Ensures path is included in a directory.

Returns the original path as {:ok, path} or an error tuple.

Example

iex> ensure_included("/foo/bar/buzz", "/foo/bar")
{:ok, "/foo/bar/buzz"}
iex> ensure_included("./foo", ".")
{:ok, "./foo"}
iex> {:error = s, _reason} = ensure_included("/usr", "."); s
:error
Link to this function expand_path(path) View Source
expand_path(Path.t()) :: Path.t()
Link to this function get_local_file_info(path, hashes \\ []) View Source
get_local_file_info(Path.t(), list()) :: %Belt.FileInfo{
  config: term(),
  hashes: term(),
  identifier: term(),
  modified: term(),
  size: term(),
  url: term()
}

Creates a Belt.FileInfo struct from a local file.

Link to this function increment_key(key, increment, fun \\ &(&1 <> "_#{&3}" <> &2)) View Source
increment_key(
  String.t(),
  integer(),
  (String.t(), String.t(), integer() -> String.t())
) :: String.t()

Increments a key name.

Returns the given key incremented by increment steps using an optional fun. fun receives the parameters base, ext and increment.

Examples

iex> increment_key("foo.tar.gz", 1)
"foo_1.tar.gz"
iex> increment_key("foo.bar", 0)
"foo.bar"
iex> increment_key("foo", 1)
"foo_1"
iex> increment_key("foo.bar", 1, fn(base, ext, increment) ->
...>   base <> ext <> " (#{increment})"
...> end)
"foo.bar (1)"
Link to this function increment_path(path, increments) View Source
increment_path(Path.t(), integer()) :: Path.t()

Increments the key part of a path.

Example

iex> increment_path("/foo/bar.ext", 1)
"/foo/bar_1.ext"
Link to this function split_key(key) View Source
split_key(String.t()) :: {String.t(), String.t()}

Splits a key into its base name and extension.

Unlike Path.extname, this also accounts for composite extensions such as .tar.gz.

Returns {base, ext} tuple.

Examples

iex> split_key("foo.bar")
{"foo", ".bar"}
iex> split_key("foo.tar.gz")
{"foo", ".tar.gz"}