Skip to content

Commit

Permalink
Update README.md to reflect change from applicatin to supervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
preciz committed Jul 13, 2024
1 parent 39c29e8 commit d50f8d2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## v0.3.0

* Breaking: Tmp is no longer an application
* Breaking: Removed `keep` functionality

## v0.2.0
Expand Down
40 changes: 26 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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

Expand All @@ -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}
Expand All @@ -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).
Expand Down

0 comments on commit d50f8d2

Please sign in to comment.