Skip to content

Decimal Support [Or Custom Type Support Documentation] #149

Open
@bmurithi

Description

@bmurithi

Noticed that :decimal ecto native type is not supported but managed to get it working via a custom ecto type.

Where is the best place to include this? README or module docs?

defmodule DecimalType do
  
    @behaviour Ecto.Type
    def type, do: :string
  
    def cast(term) when is_binary(term) or is_number(term) do
      {:ok, Decimal.new(term)}
    end
    def cast(_), do: :error
  
    def dump(%Decimal{} = term) do
      {:ok, Decimal.to_string(term)}
    end
    def dump(_), do: :error
  
    def load(term) when is_binary(term) or is_number(term) do
      {:ok, Decimal.new(term)}
    end
    def load(_), do: :error
  
  end

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions