Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
tristanperalta committed Jul 26, 2020
0 parents commit a3626b7
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Used by "mix format"
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"]
]
24 changes: 24 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# The directory Mix will write compiled artifacts to.
/_build/

# If you run "mix test --cover", coverage assets end up here.
/cover/

# The directory Mix downloads your dependencies sources to.
/deps/

# Where third-party dependencies like ExDoc output generated docs.
/doc/

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump

# Also ignore archive artifacts (built via "mix archive.build").
*.ez

# Ignore package tarball (built via "mix hex.build").
exledger-*.tar

21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Exledger

**TODO: Add description**

## Installation

If [available in Hex](https://hex.pm/docs/publish), the package can be installed
by adding `exledger` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[
{:exledger, "~> 0.1.0"}
]
end
```

Documentation can be generated with [ExDoc](https://github.com/elixir-lang/ex_doc)
and published on [HexDocs](https://hexdocs.pm). Once published, the docs can
be found at [https://hexdocs.pm/exledger](https://hexdocs.pm/exledger).

13 changes: 13 additions & 0 deletions lib/account.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule ExLedger.Account do

defstruct ~w[name sub_accounts]a

@type t :: %__MODULE__{
name: String.t(),
sub_accounts: [__MODULE__.t()]
}

def new(attrs) do
struct!(%__MODULE__{}, attrs)
end
end
13 changes: 13 additions & 0 deletions lib/amount.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule ExLedger.Amount do

defstruct ~w[quantity currency]a

@type t :: %__MODULE__{
quantity: integer(),
currency: tuple() | String.t()
}

def new(attrs) do
struct!(%__MODULE__{}, attrs)
end
end
23 changes: 23 additions & 0 deletions lib/entry.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
defmodule ExLedger.Entry do

defstruct [:date, :status, :description, transactions: []]

alias ExLedger.Transaction

@type t :: %__MODULE__{
date: DateTime.t(),
status: bool(),
description: String.t(),
transactions: [Transaction.t()]
}

def new(attrs) do
struct!(%__MODULE__{}, attrs)
end

def transactions(entries) do
entries
|> Enum.map(&(&1.transactions))
|> List.flatten()
end
end
4 changes: 4 additions & 0 deletions lib/exledger.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule Exledger do


end
27 changes: 27 additions & 0 deletions lib/ledger.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
defmodule ExLedger.Ledger do

defstruct entries: []

alias ExLedger.Entry

@type t :: %__MODULE__{
entries: [Entry.t()],
}

def new(attrs) do
struct!(%__MODULE__{}, attrs)
end

@spec balance(__MODULE__.t()) :: integer()
def balance(%{entries: entries} = _ledger) do
entries
|> Entry.transactions()
|> sum()
end

def sum(transactions) do
transactions
|> Enum.map(&(&1.amount.quantity))
|> Enum.sum()
end
end
15 changes: 15 additions & 0 deletions lib/transaction.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
defmodule ExLedger.Transaction do

defstruct ~w[account amount]a

alias ExLedger.{Account, Amount}

@type t :: %__MODULE__{
account: Account.t(),
amount: Amount.t(),
}

def new(attrs) do
struct!(%__MODULE__{}, attrs)
end
end
28 changes: 28 additions & 0 deletions mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
defmodule Exledger.MixProject do
use Mix.Project

def project do
[
app: :exledger,
version: "0.1.0",
elixir: "~> 1.10",
start_permanent: Mix.env() == :prod,
deps: deps()
]
end

# Run "mix help compile.app" to learn about applications.
def application do
[
extra_applications: [:logger]
]
end

# Run "mix help deps" to learn about dependencies.
defp deps do
[
# {:dep_from_hexpm, "~> 0.3.0"},
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
]
end
end
22 changes: 22 additions & 0 deletions test/exledger_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
defmodule ExledgerTest do
use ExUnit.Case

alias ExLedger.{Ledger, Entry, Transaction, Account, Amount}

test "Ledger.balance/1" do
account = Account.new(name: "assets")
amount = Amount.new(quantity: 1, currency: :USD)
txn1 = Transaction.new(account: account, amount: amount)

account2 = Account.new(name: "expenses")
amount2 = Amount.new(quantity: -1, currency: :USD)
txn2 = Transaction.new(account: account2, amount: amount2)

entry = Entry.new(date: DateTime.utc_now(), transactions: [txn1, txn2])

ledger = Ledger.new(entries: [entry])

# IO.inspect(ledger)
assert Ledger.balance(ledger) == 0
end
end
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ExUnit.start()

0 comments on commit a3626b7

Please sign in to comment.