-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
141c848
commit 93b51de
Showing
10 changed files
with
70 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Exledger | ||
# ExLedger | ||
|
||
**TODO: Add description** | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,45 @@ | ||
defmodule ExLedger.Amount do | ||
defstruct ~w[quantity currency]a | ||
alias Decimal, as: D | ||
|
||
defstruct [:currency, number: D.new(0)] | ||
|
||
@type t :: %__MODULE__{ | ||
quantity: integer(), | ||
currency: tuple() | String.t() | ||
number: integer(), | ||
currency: String.t() | ||
} | ||
|
||
def new(attrs) do | ||
struct!(%__MODULE__{}, attrs) | ||
def new({num, curr}), do: new(D.new(num), curr) | ||
def new(params), do: struct!(__MODULE__, params) | ||
def new(num, curr), do: new(%{number: D.new(num), currency: curr}) | ||
|
||
def to_string(%__MODULE__{} = amount) do | ||
[D.to_string(amount.number), amount.currency] | ||
|> Enum.reject(&(&1 == nil)) | ||
|> Enum.join(" ") | ||
end | ||
|
||
def eq?(%{currency: curr} = amount, %{currency: curr} = other_amount) do | ||
D.eq?(amount.number, other_amount.number) | ||
end | ||
|
||
def new(qty, currency) do | ||
new(%{quantity: qty, currency: currency}) | ||
def eq?(_, _), do: false | ||
|
||
def add(a, b), do: apply_op(:add, a, b) | ||
def sub(a, b), do: apply_op(:sub, a, b) | ||
def mult(a, b), do: apply_op(:mult, a, b) | ||
def div(a, b), do: apply_op(:div, a, b) | ||
|
||
defp apply_op(oper, %{currency: c} = a, %{currency: c} = b) do | ||
new(apply(D, oper, [a.number, b.number]), c) | ||
end | ||
|
||
defp apply_op(oper, _, _) do | ||
raise(ArithmeticError, "Unmatching currencies for #{oper} operation") | ||
end | ||
|
||
def zero(), do: new(0, nil) | ||
|
||
def negate(%__MODULE__{number: number} = amount) do | ||
%{amount | number: D.negate(number)} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
defmodule Exledger do | ||
defmodule ExLedger do | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
%{ | ||
"decimal": {:hex, :decimal, "2.0.0", "a78296e617b0f5dd4c6caf57c714431347912ffb1d0842e998e9792b5642d697", [:mix], [], "hexpm", "34666e9c55dea81013e77d9d87370fe6cb6291d1ef32f46a1600230b1d44f577"}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
defmodule ExledgerTest do | ||
defmodule ExLedgerTest do | ||
use ExUnit.Case | ||
use ExLedger.LedgerBuilder | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters