Belt v0.4.0 Belt.Provider.Helpers

Provides functions shared across providers.

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

Functions

ensure_included(path, directory)
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
expand_path(path)
expand_path(Path.t) :: Path.t
get_local_file_info(path, hashes \\ [])
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.

increment_key(key, increment, fun \\ &(&1 <> "_#{&3}" <> &2))
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)"
increment_path(path, increments)
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"
split_key(key)
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"}