trunk v0.0.3 Trunk.State View Source
This module defines a Trunk.State
struct and provides some helper functions for working with that state.
Link to this section Summary
Functions
Assigns a value to a key on the state
Retrieves an assign value for a specific version
Puts an error into the error map
Restore a saved state from a filename, JSON, or a map
Extracts the data needed from the state in order to reconstruct the file paths in future
Link to this section Types
Link to this type
t()
View Source
t() :: %Trunk.State{assigns: map, async: boolean, errors: Keyword.t, extname: String.t, filename: String.t, lower_extname: String.t, module: atom, opts: opts, path: String.t, rootname: String.t, scope: map | struct, storage: atom, storage_opts: Keyword.t, version_timeout: integer, versions: [atom] | Keyword.t}
Link to this section Functions
Link to this function
assign(state, key, value)
View Source
assign(state :: Trunk.State.t, key :: any, value :: any) :: map
Assigns a value to a key on the state.
Example:
iex> state.assigns[:hello]
nil
iex> state = Trunk.State.assign(state, :hello, :world)
iex> state.assigns[:hello]
:world
Link to this function
get_version_assign(map, version, assign)
View Source
get_version_assign(state :: Trunk.State.t, version, assign :: atom) :: any | nil
Retrieves an assign value for a specific version.
Example:
iex> state = %Trunk.State{versions: %{thumbnail: %Trunk.VersionState{assigns: %{hello: :world}}}}
iex> %Trunk.State.get_version_assign(state, :thumbnail, :hello)
:world
iex> %Trunk.State.get_version_assign(state, :thumbnail, :unknown)
nil
Puts an error into the error map.
Example:
iex> state.errors
nil
iex> state = Trunk.State.put_error(state, :thumb, :transform, "Error with convert blah blah")
iex> state.errors
%{thumb: [transform: "Error with convert blah blah"]}
Restore a saved state from a filename, JSON, or a map
Example:
iex> Trunk.State.restore("photo.jpg")
%Trunk.State{filename: "photo.jpg"}
iex> Trunk.State.restore(%{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
iex> Trunk.State.restore(%{"filename" => "photo.jpg", "assigns" => %{"hash" => "abcdef"}}
%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
iex> Trunk.State.restore("{\"filename\": \"photo.jpg\", \"assigns\": {\"hash\": \"abcdef\"}}")
%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
Link to this function
save(state, opts \\ [])
View Source
save(Trunk.State.t, [{:as, :string} | save_opts]) :: String.t
save(Trunk.State.t, [{:as, :json} | save_opts]) :: String.t
save(Trunk.State.t, [{:as, :map} | save_opts]) :: map
Extracts the data needed from the state in order to reconstruct the file paths in future.
Options:
:as
- How to save the state.:string
- Default, will just save the file name. An error will be raised if there are any assigns unless:ignore_assigns
is set to tru:map
- will save a map with keys:filename
,:assigns
, and:version_assigns
:json
- will save a map encoded as JSON (Requires Poison library to be included in deps)
:ignore_assigns
boolean, default false. Use this to save as string and ignore any assigns (Make sure you’re not using assigns forTrunk.storage_dir/2
orTrunk.filename/2
):assigns
- a list of keys to save from the assigns hashes
Example:
iex> Trunk.State.save(%Trunk.State{filename: "photo.jpg"})
"photo.jpg"
iex> Trunk.State.save(%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef"}}, as: :map)
%{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
iex> Trunk.State.save(%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef", file_size: 12345}}, as: :map, assigns: [:hash])
%{filename: "photo.jpg", assigns: %{hash: "abcdef"}}
iex> Trunk.State.save(%Trunk.State{filename: "photo.jpg", assigns: %{hash: "abcdef"}}, as: :json)
"{\"filename\": \"photo.jpg\", \"assigns\": {\"hash\": \"abcdef\"}}"