View Source YaBTT.CustomTypes.IPAddress (YaBTT v0.1.5)
A custom type for IP addresses.
Link to this section Summary
Functions
Dumps the IP address to the database.
Callback implementation for Ecto.Type.embed_as/1
.
Callback implementation for Ecto.Type.equal?/2
.
Loads the IP address from the database.
Returns the underlying schema type for the custom type.
Link to this section Types
@type io_ip_addr() :: <<_::32>> | <<_::128>>
@type ip_addr() :: :inet.ip_address()
Link to this section Functions
@spec cast(binary() | charlist()) :: :error | {:ok, ip_addr()}
@spec cast(ip_addr()) :: {:ok, ip_addr()}
Casts the given value to an IP address (ip_addr/0
).
There are two situations where this callback is called:
- When casting values by Ecto.Changeset
- When passing arguments to Ecto.Query
It will return :error
if the given term cannot be cast.
parameters
Parameters
ip
- The IP address to cast. The Ip address could be a binary, a charlist, or anip_addr/0
.
examples
Examples
iex> YaBTT.CustomTypes.IPAddress.cast('127.0.0.1')
{:ok, {127, 0, 0, 1}}
iex> YaBTT.CustomTypes.IPAddress.cast("::1")
{:ok, {0, 0, 0, 0, 0, 0, 0, 1}}
iex> YaBTT.CustomTypes.IPAddress.cast({127, 0, 0, 1})
{:ok, {127, 0, 0, 1}}
iex> YaBTT.CustomTypes.IPAddress.cast("abc")
:error
@spec dump(ip_addr()) :: :error | {:ok, io_ip_addr()}
Dumps the IP address to the database.
This callback is called when dumping values to the database.
It will return :error
if the given term cannot be dumped.
parameters
Parameters
ip
- The IP address to dump to the database. It should be anip_addr/0
.
examples
Examples
iex> YaBTT.CustomTypes.IPAddress.dump({127, 0, 0, 1})
{:ok, <<127, 0, 0, 1>>}
iex> YaBTT.CustomTypes.IPAddress.dump({0, 0, 0, 0, 0, 0, 0, 1})
{:ok, <<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1>>}
iex> YaBTT.CustomTypes.IPAddress.dump({"a", "b", "c"})
:error
Callback implementation for Ecto.Type.embed_as/1
.
Callback implementation for Ecto.Type.equal?/2
.
@spec load(io_ip_addr()) :: :error | {:ok, ip_addr()}
Loads the IP address from the database.
This callback is called when loading values from the database.
It will return :error
if the given term cannot be loaded.
parameters
Parameters
ip
- The IP address to load from the database. It is should be a binary in the normal case.
examples
Examples
iex> YaBTT.CustomTypes.IPAddress.load(<<127, 0, 0, 1>>)
{:ok, {127, 0, 0, 1}}
iex> YaBTT.CustomTypes.IPAddress.load(<<0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1>>)
{:ok, {0, 0, 0, 0, 0, 0, 0, 1}}
iex> YaBTT.CustomTypes.IPAddress.load("abc")
:error
@spec type() :: :binary
Returns the underlying schema type for the custom type.