View Source Ash.Type.File (ash v3.4.46)
A type that represents a file on the filesystem.
Persistence
This type does not support persisting via Ash.DataLayer
.
It is mainly intended to be used in arguments.
Valid values to cast
This type can cast multiple types of values:
- itself
Plug.Upload
- Any value that implements the
Ash.Type.File.Source
protocol.
Summary
Functions
Create a file from an IO.device()
Create a file from a path.
Open the file with the given modes
.
Returns the path to the file.
Types
@type t() :: %Ash.Type.File{ implementation: Ash.Type.File.Implementation.t(), source: Ash.Type.File.Implementation.source() }
Functions
Create a file from an IO.device()
Example
iex> path = "README.md"
...> {:ok, device} = File.open(path)
...> Ash.Type.File.from_io(device)
%Ash.Type.File{source: device, implementation: Ash.Type.File.IO}
Create a file from a path.
Example
iex> path = "README.md"
...> Ash.Type.File.from_path(path)
%Ash.Type.File{source: "README.md", implementation: Ash.Type.File.Path}
@spec open(file :: t(), modes :: [File.mode()]) :: {:ok, IO.device()} | {:error, Ash.Type.File.Implementation.error()}
Open the file with the given modes
.
This function will delegate to the open/2
function on the implementation
.
For details on the modes
argument, see the File.open/2
documentation.
Example
iex> path = "README.md"
...> file = Ash.Type.File.from_path(path)
...> Ash.Type.File.open(file, [:read])
...> # => {:ok, #PID<0.109.0>}
@spec path(file :: t()) :: {:ok, Path.t()} | {:error, :not_supported | Ash.Type.File.Implementation.error()}
Returns the path to the file.
Not every implementation will support this operation. If the implementation
does not support this operation, then {:error, :not_supported}
will be
returned. In this case, use the open/2
function to access the file.
Example
iex> path = "README.md"
...> file = Ash.Type.File.from_path(path)
...> Ash.Type.File.path(file)
{:ok, "README.md"}