Packages

Tiny Gleam dependency injection. Make IO functions unit tests friendly.

Current section

Files

Jump to
glency src glency.erl
Raw

src/glency.erl

-module(glency).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-export([init_di/0, di/3, with_di/3]).
-spec init_di() -> carpenter@table:set(any(), any()).
init_di() ->
_assert_subject = begin
_pipe = carpenter@table:build(<<"glency"/utf8>>),
_pipe@1 = carpenter@table:privacy(_pipe, public),
_pipe@2 = carpenter@table:write_concurrency(
_pipe@1,
auto_write_concurrency
),
_pipe@3 = carpenter@table:read_concurrency(_pipe@2, true),
_pipe@4 = carpenter@table:decentralized_counters(_pipe@3, true),
_pipe@5 = carpenter@table:compression(_pipe@4, false),
carpenter@table:set(_pipe@5)
end,
{ok, Set} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"glency"/utf8>>,
function => <<"init_di"/utf8>>,
line => 9})
end,
Set.
-spec di(binary(), any(), fun(() -> KWA)) -> KWA.
di(Key, Args, Cb) ->
_assert_subject = carpenter@table:ref(<<"glency"/utf8>>),
{ok, Set} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"glency"/utf8>>,
function => <<"di"/utf8>>,
line => 23})
end,
case begin
_pipe = carpenter@table:lookup(Set, <<"glency"/utf8>>),
gleam@list:first(_pipe)
end of
{ok, {_, Cache}} ->
case gleam@dict:get(Cache, Key) of
{ok, Cb@1} ->
_assert_subject@1 = begin
_pipe@1 = Args,
gluple@reflect:tuple_to_list(_pipe@1)
end,
{ok, Args@1} = case _assert_subject@1 of
{ok, _} -> _assert_subject@1;
_assert_fail@1 ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail@1,
module => <<"glency"/utf8>>,
function => <<"di"/utf8>>,
line => 28})
end,
_pipe@2 = Args@1,
gleaf:apply(_pipe@2, Cb@1);
{error, nil} ->
Cb()
end;
{error, nil} ->
Cb()
end.
-spec with_di(binary(), any(), fun(() -> any())) -> nil.
with_di(Key, Mock, Cb) ->
_assert_subject = carpenter@table:ref(<<"glency"/utf8>>),
{ok, Set} = case _assert_subject of
{ok, _} -> _assert_subject;
_assert_fail ->
erlang:error(#{gleam_error => let_assert,
message => <<"Assertion pattern match failed"/utf8>>,
value => _assert_fail,
module => <<"glency"/utf8>>,
function => <<"with_di"/utf8>>,
line => 40})
end,
case begin
_pipe = carpenter@table:lookup(Set, <<"glency"/utf8>>),
gleam@list:first(_pipe)
end of
{ok, {_, Cache}} ->
Cache@1 = gleam@dict:insert(Cache, Key, Mock),
carpenter@table:insert(Set, [{<<"glency"/utf8>>, Cache@1}]),
Set;
{error, nil} ->
carpenter@table:insert(
Set,
[{<<"glency"/utf8>>, maps:from_list([{Key, Mock}])}]
),
Set
end,
Cb(),
carpenter@table:delete_all(Set).