Add a `Decimal` type
KristerV opened this issue · comments
Krister Viirsaar commented
i use Decimal for sensitive numbers and cloak_ecto doesn't seem to support it.
it doesn't look too hard to implement, but can someone check this? seems to be working for me.
defmodule Cloak.Ecto.Decimal do
defmacro __using__(opts) do
opts = Keyword.merge(opts, vault: Keyword.fetch!(opts, :vault))
quote do
alias Decimal, as: D
use Cloak.Ecto.Type, unquote(opts)
def cast(value), do: Ecto.Type.cast(:decimal, value)
def before_encrypt(value) do
case Ecto.Type.cast(:decimal, value) do
{:ok, d} -> D.to_string(d)
_error -> :error
end
end
def after_decrypt(value) do
case D.new(value) do
%D{} = d -> d
_error -> :error
end
end
end
end
end