Packages

Dimensional analysis and unit conversions for Elixir.

Current section

Files

Jump to
ex_dimensions lib ex_dimensions ecto.ex
Raw

lib/ex_dimensions/ecto.ex

defmodule ExDimensions.Ecto.UnitField do
@moduledoc """
A custom Ecto type for integration into Ecto based projects.
The column is defined as a `:map` Ecto type with 2 keys:
units: string representation of the units
value: numerical value of the quantity
For Postgres databases, that will map to a `jsonb` column.
Other database implementations will vary.
"""
@moduledoc since: "0.1.0"
use Ecto.Type
def type, do: :map
def cast(%ExDimensions.Quantity{} = quantity), do: {:ok, quantity}
def cast(%{"denom" => denom, "units" => units, "value" => value}) do
{:ok, %ExDimensions.Quantity{denom: denom, units: units, value: value}}
end
def load(data) when is_map(data) do
%{units: units, denom: denom} = parse(data["units"])
{:ok, %ExDimensions.Quantity{value: data["value"], units: units, denom: denom}}
end
def dump(%ExDimensions.Quantity{value: v} = quantity) do
{:ok, %{value: v, units: to_string(quantity) |> String.split(" ") |> List.last()}}
end
def parse(unit_str) do
{:ok, parsed, "", _, _, _} = ExDimensions.Parser.units(unit_str)
parsed
|> ExDimensions.Parser.extract()
end
end