CssColors v0.2.2 CssColors.Ecto.Color View Source
Custom Ecto type for representing CSS colors.
The colors are stored as CSS colors strings in the data store. They may be any valid CSS colors. Note that you can not expect the stored colors to be in any specific format (rgb/hsl etc), they may be in any supported color scheme.
If you define a max length on the data store field, make sure any possible color can fit by setting the length to at least 30 characters.
Example:
defmodule User do
use Ecto.Schema
schema "users" do
field :favourite_color, CssColors.Ecto.Color
end
def changeset(struct, params) do
struct
|> cast(params, [:favourite_color])
end
end
Link to this section Summary
Functions
Casts the given input to the custom type
Dumps the given term into an Ecto native type
Loads the given term into a custom type
Returns the underlying schema type for the custom type
Link to this section Functions
cast(string)
View Source
cast(String.t() | CssColors.color()) :: {:ok, CssColors.color()} | :error
cast(String.t() | CssColors.color()) :: {:ok, CssColors.color()} | :error
Casts the given input to the custom type.
This callback is called on external input and can return any type,
as long as the dump/1 function is able to convert the returned
value into an Ecto native type. There are two situations where
this callback is called:
- When casting values by
Ecto.Changeset - When passing arguments to
Ecto.Query
When returning {:error, keyword()}, the returned keyword list
will be preserved in the changeset errors, similar to
Changeset.add_error/4. Passing a :message key, will override
the default message. It is not possible to override the :type key.
For {:array, CustomType} or {:map, CustomType} the returned
keyword list will be erased and the default error will be shown.
Callback implementation for Ecto.Type.cast/1.
dump(color)
View Source
dump(CssColors.color()) :: {:ok, String.t()}
dump(CssColors.color()) :: {:ok, String.t()}
Dumps the given term into an Ecto native type.
This callback is called with any term that was stored in the struct and it needs to validate them and convert it to an Ecto native type.
Callback implementation for Ecto.Type.dump/1.
load(string)
View Source
load(String.t()) :: {:ok, CssColors.color()}
load(String.t()) :: {:ok, CssColors.color()}
Loads the given term into a custom type.
This callback is called when loading data from the database and
receive an Ecto native type. It can return any type, as long as
the dump/1 function is able to convert the returned value back
into an Ecto native type.
Callback implementation for Ecto.Type.load/1.
type() View Source
Returns the underlying schema type for the custom type.
For example, if you want to provide your own date
structures, the type function should return :date.
Note this function is not required to return Ecto primitive types, the type is only required to be known by the adapter.
Callback implementation for Ecto.Type.type/0.