Skip to content

Instantly share code, notes, and snippets.

View rugyoga's full-sized avatar

Guy Argo rugyoga

View GitHub Profile
@rugyoga
rugyoga / delta_simple.ex
Created September 4, 2022 22:26
Take the delta of two simple velues
@spec delta_simple(path(), term(), term()) :: [delta()]
defp delta_simple(_path, a, b) when a == b, do: []
defp delta_simple(path, a, b) do
cond do
empty?(a) -> [{path, {:add, b}}]
empty?(b) -> [{path, {:delete, a}}]
true -> [{path, {:update, a, b}}]
end
end
@rugyoga
rugyoga / empty.ex
Created September 4, 2022 22:28
Is this value empty?
@empty [false, "", nil, [], 0, 0.0]
@spec empty?(any()) :: boolean()
def empty?(value) when value in @empty, do: true
def empty?(value) when is_list(value), do: Enum.all?(value, &empty?/1)
def empty?(value) when is_tuple(value), do: value |> Tuple.to_list() |> empty?()
def empty?(value) when is_struct(value), do: value |> Map.from_struct() |> empty?()
def empty?(value) when is_map(value), do: value |> Map.values() |> empty?()
def empty?(_), do: false
@rugyoga
rugyoga / audit_types.ex
Created September 5, 2022 01:05
types and constants for audit
@key :__audit_trail__
@enabled? Application.compile_env(:audit, :enabled?, false)
@type file_t :: binary()
@type line_t :: non_neg_integer()
@type trail_t :: {struct(), file_t(), line_t()}
@type change_t :: {struct(), trail_t()}
@spec audit_fun(struct(), Macro.Env.t()) :: struct()
def audit_fun(r, e) do
r |> struct([{@key, payload(r, e)}])
end
@spec payload(struct(), Macro.Env) :: trail_t()
def payload(r, e), do: {r, e.file, e.line}
@rugyoga
rugyoga / audit_macro.ex
Created September 5, 2022 03:26
Audit macro
defmacro audit(record) do
quote generated: true do
unquote(__MODULE__).audit_fun(unquote(record), __ENV__)
end
end
@rugyoga
rugyoga / changelist.ex
Created September 5, 2022 03:31
Change list
@spec changelist(term) :: [change_t()]
defp changelist(r = %_{__audit_trail__: audit_trail}) do
if audit_trail, do: [{r, audit_trail} | changelist(record(audit_trail))], else: []
end
defp changelist(_), do: []
def to_string(r) do
r
|> changelist()
|> Enum.map_join("\n=====\n", &stringify_change/1)
end
@spec changelist(term) :: [change_t()]
defp changelist(r = %_{__audit_trail__: audit_trail}) do
if audit_trail, do: [{r, audit_trail} | changelist(record(audit_trail))], else: []
end
@rugyoga
rugyoga / file_cache.ex
Created September 6, 2022 17:30
Agent that performs file cacheing
defmodule Audit.FileCache do
@moduledoc """
Simple File cache Agent
"""
use Agent
def start_link(_args) do
Agent.start_link(fn -> %{} end, name: __MODULE__)
end
@rugyoga
rugyoga / github.ex
Created September 6, 2022 17:34
Module to wrangle GitHub URLs.
defmodule Audit.Github do
@moduledoc """
Helper functions for generating Github URLs from filename and line number
"""
@type line_number_t :: binary | non_neg_integer()
@type filename_t :: binary()
@type git_t :: binary() | nil
@spec git([binary()]) :: git_t()
@rugyoga
rugyoga / iex_example.ex
Last active September 7, 2022 17:31
iex example
iex> nil |> Tree.add(2) |> Tree.add(1) |> Tree.add(3) |> Audit.to_string() |> IO.puts
github url: https://github.com/rugyoga/tree_ex/tree/main/lib/tree.ex#L15
local path: lib/tree.ex:15
code: item > i -> %Tree{audit(tree) | right: add(r, item)}
diff: [{[:right], {:add, %Tree{item: 3, left: nil, right: nil}}}]
=====
github url: https://github.com/rugyoga/tree_ex/tree/main/lib/tree.ex#L14
local path: lib/tree.ex:14
code: item < i -> %Tree{audit(tree) | left: add(l, item)}
diff: [{[:left], {:add, %Tree{item: 1, left: nil, right: nil}}}]