Skip to content

Instantly share code, notes, and snippets.

View noelworden's full-sized avatar

Noel Worden noelworden

View GitHub Profile
iex> Decimal.mult(Decimal.cast("0.8765309"), 100)
#Decimal<87.6530900>
Decimal.mult(Decimal.cast(example_decimal), 100)
defp mathematical_validation(changeset) do
revenue = get_field(changeset, :revenue)
expense = get_field(changeset, :expense)
net_gain = get_field(changeset, :net_gain)
case Decimal.sub(revenue, expense) == Decimal.new(net_gain) do
true -> changeset
_ -> add_error(changeset, :net_gain, "must equal revenue minus expense")
end
end
def validate(changeset) do
changeset
|> validate_required([:revenue, :expense, :net_gain])
|> mathematical_validation()
|> validate_format(:reference_id, ~r/(^\d+$|\p{L})/u,
message: "must be a number or contain a letter"
)
end
defp mathematical_validation(changeset) do
revenue = get_field(changeset, :revenue)
expense = get_field(changeset, :expense)
net_gain = get_field(changeset, :net_gain)
with {:is_valid, true} <- {:is_valid, changeset.valid?},
{math_validation, true} <-
{math_validation, Decimal.sub(revenue, expense) == Decimal.new(net_gain)} do
changeset
else
def validate(changeset) do
changeset
|> validate_required([:revenue, :expense, :net_gain])
|> mathematical_validation()
|> validate_format(:reference_id, ~r/(^\d+$|\p{L})/u,
message: "must be a number or contain a letter"
)
end
def validate(changeset) do
changeset
|> validate_required([:revenue, :expense, :net_gain, :first_name])
|> mathematical_validation()
|> validate_format(:reference_id, ~r/(^\d+$|\p{L})/u,
message: "must be a number or contain a letter"
)
end
defp mathematical_validation(changeset) do
defp mathematical_validation(changeset) do
revenue = get_field(changeset, :revenue)
expense = get_field(changeset, :expense)
net_gain = get_field(changeset, :net_gain)
with true <- revenue != nil,
true <- expense != nil,
true <- net_gain != nil,
true <- Decimal.sub(revenue, expense) == Decimal.new(net_gain) do
changeset
defmodule Finance.Schemas.User do
  @moduledoc """
  The location of this schema has changed from the default placement upon
  installation (`mix pow.install`). If using the Pow library docs to make any
import Ecto.Query
alias MyApp.{
Repo,
Schemas.User,
Schemas.Posts,
Schemas.Comments,
}
example_variable = ["a", "b", "c"]