diff --git a/CHANGELOG.md b/CHANGELOG.md index e74b514..ef0a14d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## v0.3.0 +* Breaking: Tmp is no longer an application * Breaking: Removed `keep` functionality ## v0.2.0 diff --git a/README.md b/README.md index 6c56451..29416b6 100644 --- a/README.md +++ b/README.md @@ -18,17 +18,37 @@ end ## Usage -`Tmp.dir/2` accepts a function that will be called with the path of a new temporary directory. -The temporary directory is automatically removed when the function returns or the calling process exits. +Define your Tmp module: ```elixir -Tmp.dir(fn tmp_dir_path -> - # ... do work with tmp_dir_path +defmodule MyApp.Tmp do + use Tmp +end +``` + +Add it to your supervision tree: + +```elixir +children = [ + {MyApp.Tmp, name: MyApp.Tmp} +] +``` + +Use it in your code: + +```elixir +MyApp.Tmp.dir(fn tmp_dir_path -> + file_path = Path.join(tmp_dir_path, "my_file") + # do work with file_path... + # then return a value + {:ok, :work_done} end) ``` ### Options +When calling `MyApp.Tmp.dir/2`, you can pass the following options: + - `:prefix` (optional) - Prefix for the temporary directory name, defaults to `nil` - `:base_dir` (optional) - Base directory for the temporary directory, defaults to `System.tmp_dir()` - `:timeout` (optional) - Timeout in milliseconds, defaults to `:infinity` @@ -38,7 +58,7 @@ end) Basic usage: ```elixir -Tmp.dir(fn tmp_dir_path -> +MyApp.Tmp.dir(fn tmp_dir_path -> File.touch(Path.join(tmp_dir_path, "file_one")) # ... other important work @@ -50,7 +70,7 @@ end, prefix: "my_app", base_dir: "/tmp/custom_base") Error handling: ```elixir -Tmp.dir(fn tmp_dir_path -> +MyApp.Tmp.dir(fn tmp_dir_path -> case work(tmp_dir_path) do {:ok, result} -> {:ok, result} @@ -62,14 +82,6 @@ Tmp.dir(fn tmp_dir_path -> end) ``` -## Config - -(Optional) To configure the default base directory: - -```elixir -config :tmp, default_base_dir: "/tmp/my_custom_dir" -``` - ## Docs Documentation can be found at [https://hexdocs.pm/tmp](https://hexdocs.pm/tmp).