View Source Pageantry.Cast (Pageantry v0.6.0)
Helper functions for casting user filter input to schema-defined types (which can then be used in ecto filter queries).
Summary
Functions
Casts the specified value to binary.
Casts the specified value to a boolean.
Casts the specified value to a date.
Casts the specified value to a datetime.
Casts the specified value to a decimal.
Casts the specified value to a float.
Casts the specified value to a Postgrex.INET
.
Casts the specified value to an integer.
Casts the specified value to a naive datetime.
Casts the specified value to a string.
Casts the specified value to a time.
Casts the specified value to the specified type.
Functions
@spec cast_to_binary(term(), binary(), Pageantry.Prefs.t()) :: binary()
Casts the specified value to binary.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{}
iex> cast_to_binary(true, prefs)
"true"
iex> cast_to_binary(-1.9, prefs)
"-1.9"
iex> cast_to_binary(:atom, prefs)
"atom"
iex> cast_to_binary("foo", prefs)
"foo"
iex> cast_to_binary([1,2,3], prefs)
<<1, 2, 3>>
iex> cast_to_binary({1,2,3}, prefs)
nil
@spec cast_to_boolean(term(), boolean(), Pageantry.Prefs.t()) :: boolean()
Casts the specified value to a boolean.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{}
iex> cast_to_boolean(true, prefs)
true
iex> cast_to_boolean("TRUE", prefs)
true
iex> cast_to_boolean("f", prefs)
false
iex> cast_to_boolean("yes", prefs)
true
iex> cast_to_boolean("?", prefs)
nil
iex> cast_to_boolean(1.5, prefs)
true
iex> cast_to_boolean(nil, true, prefs)
true
@spec cast_to_date(term(), Date.t(), Pageantry.Prefs.t()) :: Date.t() | Date.Range.t()
Casts the specified value to a date.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{now: fn _ -> {:ok, ~U[2001-02-03 04:05:06Z]} end}
iex> cast_to_date(~D[2001-02-03], prefs)
~D[2001-02-03]
iex> cast_to_date(~N[2001-02-03 04:05:06], prefs)
~D[2001-02-03]
iex> cast_to_date(~U[2001-02-03 04:05:06Z], prefs)
~D[2001-02-03]
iex> cast_to_date(%{first: ~D[2001-02-03], last: ~D[2001-02-04]}, prefs)
Date.range(~D[2001-02-03], ~D[2001-02-04])
iex> cast_to_date("today", prefs)
Date.range(~D[2001-02-03], ~D[2001-02-03])
@spec cast_to_datetime(term(), DateTime.t(), Pageantry.Prefs.t()) :: DateTime.t() | %{first: DateTime.t(), last: DateTime.t()}
Casts the specified value to a datetime.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{tz: "Etc/UTC", now: fn _ -> {:ok, ~U[2001-02-03 04:05:06Z]} end}
iex> cast_to_datetime(~N[2001-02-03 04:05:06], prefs)
~U[2001-02-03 04:05:06Z]
iex> cast_to_datetime(~U[2001-02-03 04:05:06Z], prefs)
~U[2001-02-03 04:05:06Z]
iex> cast_to_datetime(~D[2001-02-03], prefs)
~U[2001-02-03 00:00:00Z]
iex> cast_to_datetime(%{first: ~D[2001-02-03], last: ~D[2001-02-04]}, prefs)
%{first: ~U[2001-02-03 00:00:00Z], last: ~U[2001-02-04 00:00:00Z]}
iex> cast_to_datetime("today", prefs)
%{first: ~U[2001-02-03 00:00:00Z], last: ~U[2001-02-03 23:59:59Z]}
@spec cast_to_decimal(term(), Decimal.t(), Pageantry.Prefs.t()) :: Decimal.t() | %{first: Decimal.t(), last: Decimal.t()}
Casts the specified value to a decimal.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{}
iex> cast_to_decimal(1, prefs)
Decimal.new(1)
iex> cast_to_decimal(-1.9, prefs)
Decimal.new("-1.9")
iex> cast_to_decimal(%{first: 1.0, last: 9.0}, prefs)
%{first: Decimal.from_float(1.0), last: Decimal.from_float(9.0)}
iex> cast_to_decimal("-1.9", prefs)
Decimal.new("-1.9")
iex> cast_to_decimal("1e1", prefs)
Decimal.new("1E+1")
iex> cast_to_decimal("1-9", prefs)
%{first: Decimal.new(1), last: Decimal.new(9)}
iex> cast_to_decimal("-9 to -1.9", prefs)
%{first: Decimal.new(-9), last: Decimal.from_float(-1.9)}
iex> cast_to_decimal("?", prefs)
nil
iex> cast_to_decimal(nil, Decimal.new(3), prefs)
Decimal.new(3)
@spec cast_to_float(term(), float(), Pageantry.Prefs.t()) :: float() | %{first: float(), last: float()}
Casts the specified value to a float.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{}
iex> cast_to_float(1, prefs)
1.0
iex> cast_to_float(-1.9, prefs)
-1.9
iex> cast_to_float(%{first: 1.0, last: 9.0}, prefs)
%{first: 1.0, last: 9.0}
iex> cast_to_float("-1.9", prefs)
-1.9
iex> cast_to_float("1e1", prefs)
10.0
iex> cast_to_float("1-9", prefs)
%{first: 1.0, last: 9.0}
iex> cast_to_float("-9 to -1.9", prefs)
%{first: -9.0, last: -1.9}
iex> cast_to_float("?", prefs)
nil
iex> cast_to_float(nil, 3.0, prefs)
3.0
@spec cast_to_inet(term(), Postgrex.INET.t(), Pageantry.Prefs.t()) :: Postgrex.INET.t()
Casts the specified value to a Postgrex.INET
.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{}
iex> cast_to_inet("0.0.0.0", prefs)
%Postgrex.INET{address: {0,0,0,0}}
iex> cast_to_inet("::", prefs)
%Postgrex.INET{address: {0,0,0,0,0,0,0,0}}
iex> cast_to_inet("0.0.0.0/0", prefs)
%Postgrex.INET{address: {0,0,0,0}, netmask: 0}
iex> cast_to_inet("1", prefs)
nil
@spec cast_to_integer(term(), integer(), Pageantry.Prefs.t()) :: integer() | %{first: integer(), last: integer()}
Casts the specified value to an integer.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{}
iex> cast_to_integer(1, prefs)
1
iex> cast_to_integer(-1.9, prefs)
-1
iex> cast_to_integer(1..9, prefs)
1..9
iex> cast_to_integer("-1.9", prefs)
-1
iex> cast_to_integer("1e1", prefs)
10
iex> cast_to_integer("1-9", prefs)
1..9
iex> cast_to_integer("-9 to -1.9", prefs)
-9..-1
iex> cast_to_integer("?", prefs)
nil
iex> cast_to_integer(nil, 3, prefs)
3
@spec cast_to_naive_datetime(term(), NaiveDateTime.t(), Pageantry.Prefs.t()) :: NaiveDateTime.t() | %{first: NaiveDateTime.t(), last: NaiveDateTime.t()}
Casts the specified value to a naive datetime.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{now: fn _ -> {:ok, ~U[2001-02-03 04:05:06Z]} end}
iex> cast_to_naive_datetime(~N[2001-02-03 04:05:06], prefs)
~N[2001-02-03 04:05:06]
iex> cast_to_naive_datetime(~U[2001-02-03 04:05:06Z], prefs)
~N[2001-02-03 04:05:06]
iex> cast_to_naive_datetime(~D[2001-02-03], prefs)
~N[2001-02-03 00:00:00]
iex> cast_to_naive_datetime(%{first: ~D[2001-02-03], last: ~D[2001-02-04]}, prefs)
%{first: ~N[2001-02-03 00:00:00], last: ~N[2001-02-04 00:00:00]}
iex> cast_to_naive_datetime("today", prefs)
%{first: ~N[2001-02-03 00:00:00], last: ~N[2001-02-03 23:59:59]}
@spec cast_to_string(term(), String.t(), Pageantry.Prefs.t()) :: String.t()
Casts the specified value to a string.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{}
iex> cast_to_string(true, prefs)
"true"
iex> cast_to_string(-1.9, prefs)
"-1.9"
iex> cast_to_string(:atom, prefs)
"atom"
iex> cast_to_string("foo", prefs)
"foo"
iex> cast_to_string([1,2,3], prefs)
<<1, 2, 3>>
iex> cast_to_string({1,2,3}, prefs)
nil
@spec cast_to_time(term(), Time.t(), Pageantry.Prefs.t()) :: Time.t() | %{first: Time.t(), last: Time.t()}
Casts the specified value to a time.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{now: fn _ -> {:ok, ~U[2001-02-03 04:05:06Z]} end}
iex> cast_to_time(~T[04:05:06], prefs)
~T[04:05:06]
iex> cast_to_time(~D[2001-02-03], prefs)
~T[00:00:00]
iex> cast_to_time(~N[2001-02-03 04:05:06], prefs)
~T[04:05:06]
iex> cast_to_time(~U[2001-02-03 04:05:06Z], prefs)
~T[04:05:06]
iex> cast_to_time(%{first: ~T[00:00:00], last: ~T[23:59:59]}, prefs)
%{first: ~T[00:00:00], last: ~T[23:59:59]}
iex> cast_to_time("today", prefs)
%{first: ~T[00:00:00], last: ~T[23:59:59]}
@spec cast_to_type(atom(), term(), term(), Pageantry.Prefs.t()) :: term()
Casts the specified value to the specified type.
Examples
iex> import Pageantry.Cast
iex> prefs = %Pageantry.Prefs{now: fn _ -> {:ok, ~U[2001-02-03 04:05:06Z]} end}
iex> cast_to_type(:boolean, "yes", nil, prefs)
true
iex> cast_to_type(:integer, 2.5, nil, prefs)
2
iex> cast_to_type(:integer, "1-9", nil, prefs)
1..9
iex> cast_to_type(:integer, "foo", -1, prefs)
-1
iex> cast_to_type(:utc_datetime, "today", nil, prefs)
%{first: ~U[2001-02-03 00:00:00Z], last: ~U[2001-02-03 23:59:59Z]}