Skip to content

Commit

Permalink
add .formatter.exs and format project
Browse files Browse the repository at this point in the history
  • Loading branch information
Bentheburrito committed Jan 4, 2025
1 parent f06bc68 commit a4214bc
Show file tree
Hide file tree
Showing 30 changed files with 307 additions and 767 deletions.
4 changes: 4 additions & 0 deletions .formatter.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
[
inputs: ["{mix,.formatter}.exs", "{config,lib,test}/**/*.{ex,exs}"],
line_length: 120
]
2 changes: 1 addition & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ import Config

# Default Path to Mnesia for local development
config :mnesia,
dir: '.mnesia/#{Mix.env}/#{node()}'
dir: ~c".mnesia/#{Mix.env()}/#{node()}"
19 changes: 2 additions & 17 deletions lib/memento/errors/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,21 @@ defmodule Memento.Error do
alias Memento.DoesNotExistError
alias Memento.MnesiaException


defexception [:message]


@moduledoc false
@default_message "Operation Failed"




# Macros to Raise Errors
# ----------------------


# Raise a Memento.Error
defmacro raise(message \\ @default_message) do
quote do
raise Memento.Error, message: unquote(message)
end
end


# Finds the appropriate Memento error from an Mnesia exit
# Falls back to a default 'MnesiaException'
defmacro raise_from_code(data) do
Expand All @@ -35,18 +28,13 @@ defmodule Memento.Error do
end
end




# Error Builders
# --------------


# Helper Method to Build Memento Exceptions
def normalize({:error, reason}), do: do_normalize(reason)
def normalize({:error, reason}), do: do_normalize(reason)
def normalize({:aborted, reason}), do: do_normalize(reason)
def normalize(reason), do: do_normalize(reason)

def normalize(reason), do: do_normalize(reason)

defp do_normalize(reason) do
case reason do
Expand All @@ -60,17 +48,14 @@ defmodule Memento.Error do
{:already_exists, resource} ->
%AlreadyExistsError{message: "#{inspect(resource)} already exists"}


# Custom Error Code - Not Part of Mnesia
{:autoincrement, message} ->
%InvalidOperationError{message: "Autoincrement #{message}"}


# Don't need custom errors for the rest, fallback to MnesiaException
# and raise with Mnesia's description of the error
error ->
MnesiaException.build(error)
end
end

end
17 changes: 8 additions & 9 deletions lib/memento/errors/mnesia_exception.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,25 @@ defmodule Memento.MnesiaException do
@moduledoc false
@newline "\n "



# Build the Exception struct
def build(error) do
message =
"Mnesia operation failed" <> @newline <>
info(error) <> @newline <>
"Mnesia Error: " <> inspect(error)
"Mnesia operation failed" <>
@newline <>
info(error) <>
@newline <>
"Mnesia Error: " <> inspect(error)

%MnesiaException{
data: error,
message: message,
message: message
}
end


# Fetch Mnesia's description of the error
defp info({code, _, _}), do: info(code)
defp info({code, _}), do: info(code)
defp info({code, _}), do: info(code)

defp info(code) do
desc = :mnesia.error_description(code)

Expand All @@ -36,5 +36,4 @@ defmodule Memento.MnesiaException do
true -> inspect(desc)
end
end

end
4 changes: 0 additions & 4 deletions lib/memento/errors/simple_errors.ex
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
## Simple Error Modules
## --------------------


# NOTE TO SELF:
# Please don't try to be over-efficient and edgy by dynamically
# defining these exceptions from a list of Module names. It looks
# really fucking bad.



defmodule Memento.NoTransactionError do
@moduledoc false
defexception [:message]
Expand All @@ -28,4 +25,3 @@ defmodule Memento.InvalidOperationError do
@moduledoc false
defexception [:message]
end

3 changes: 0 additions & 3 deletions lib/memento/errors/transaction_aborted.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ defmodule Memento.TransactionAborted do

@moduledoc false


# Raise a Memento.TransactionAborted
defmacro raise(reason) do
quote(bind_quoted: [reason: reason]) do
raise Memento.TransactionAborted,
message: "Transaction aborted with: #{inspect(reason)}"
end
end

end

29 changes: 4 additions & 25 deletions lib/memento/memento.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@ defmodule Memento do
require Memento.Mnesia
require Memento.Error


@moduledoc """
Simple + Powerful interface to the Erlang Mnesia Database.
See the [README](https://hexdocs.pm/memento) to get started.
"""




# Public API
# ----------


@doc """
Start the Memento Application.
Expand All @@ -28,9 +23,6 @@ defmodule Memento do
Application.start(:mnesia)
end




@doc """
Stop the Memento Application.
"""
Expand All @@ -39,9 +31,6 @@ defmodule Memento do
Application.stop(:mnesia)
end




@doc """
Tells Memento about other nodes running Memento/Mnesia.
Expand All @@ -66,16 +55,13 @@ defmodule Memento do
def add_nodes(nodes) do
nodes = List.wrap(nodes)

if Enum.any?(nodes, & !is_atom(&1)) do
if Enum.any?(nodes, &(!is_atom(&1))) do
Memento.Error.raise("Invalid Node list passed")
end

Memento.Mnesia.call(:change_config, [:extra_db_nodes, nodes])
end




@doc """
Prints `:mnesia` information to console.
"""
Expand All @@ -84,9 +70,6 @@ defmodule Memento do
Memento.Mnesia.call(:info, [])
end




@doc """
Returns all information about the Mnesia system.
Expand All @@ -100,15 +83,11 @@ defmodule Memento do
Memento.Mnesia.call(:system_info, [key])
end




# Delegates

defdelegate wait(tables), to: Memento.Table
defdelegate wait(tables), to: Memento.Table
defdelegate wait(tables, timeout), to: Memento.Table

defdelegate transaction(fun), to: Memento.Transaction, as: :execute
defdelegate transaction!(fun), to: Memento.Transaction, as: :execute!

defdelegate transaction(fun), to: Memento.Transaction, as: :execute
defdelegate transaction!(fun), to: Memento.Transaction, as: :execute!
end
18 changes: 1 addition & 17 deletions lib/memento/mnesia.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,16 @@ defmodule Memento.Mnesia do
# via a macro, handle the result including re-raising
# any errors that have been caught.




# Helper API
# ----------


@doc "Call an Mnesia function"
defmacro call(method, arguments \\ []) do
quote(bind_quoted: [fun: method, args: arguments]) do
apply(:mnesia, fun, args)
end
end



@doc """
Call an Mnesia function and catch any exits
Expand All @@ -39,13 +33,9 @@ defmodule Memento.Mnesia do
catch
:exit, error -> Memento.Error.raise_from_code(error)
end

end
end




@doc """
Normalize the result of an :mnesia call
Expand Down Expand Up @@ -83,16 +73,12 @@ defmodule Memento.Mnesia do
end
end





# Private Helpers
# ---------------


# Check if the error is actually an exception, and reraise it
defp reraise_if_valid!(:throw, data), do: throw(data)

defp reraise_if_valid!(exception, stacktrace) do
error = Exception.normalize(:error, exception, stacktrace)

Expand All @@ -110,6 +96,4 @@ defmodule Memento.Mnesia do
nil
end
end


end
15 changes: 3 additions & 12 deletions lib/memento/query/data.ex
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ defmodule Memento.Query.Data do
```
"""




# Public API
# ----------


@doc """
Convert Mnesia data tuple into Memento Table struct.
Expand All @@ -51,7 +47,7 @@ defmodule Memento.Query.Data do
table's attributes and convert it into a struct of the Memento
table you defined.
"""
@spec load(tuple) :: Memento.Table.record
@spec load(tuple) :: Memento.Table.record()
def load(data) when is_tuple(data) do
[table | values] =
Tuple.to_list(data)
Expand All @@ -64,9 +60,6 @@ defmodule Memento.Query.Data do
struct(table, values)
end




@doc """
Convert Memento Table struct into Mnesia data tuple.
Expand All @@ -75,15 +68,13 @@ defmodule Memento.Query.Data do
table definition, and this will convert it into a tuple
representing an Mnesia record.
"""
@spec dump(Memento.Table.record) :: tuple
@spec dump(Memento.Table.record()) :: tuple
def dump(data = %{__struct__: table}) do
values =
table.__info__()
|> Map.get(:attributes)
|> Enum.map(&Map.get(data, &1))

List.to_tuple([ table | values ])
List.to_tuple([table | values])
end


end
Loading

0 comments on commit a4214bc

Please sign in to comment.