Skip to content

Commit

Permalink
Add spec for module_state_compiler module
Browse files Browse the repository at this point in the history
  • Loading branch information
shahryarjb committed Jun 16, 2024
1 parent 5168ed3 commit 76fa2d0
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/event/module_state_compiler.ex
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
defmodule MishkaInstaller.Event.ModuleStateCompiler do
@state_dir "MishkaInstaller.Event.ModuleStateCompiler.State."

@type error_return :: {:error, [%{action: atom(), field: atom(), message: String.t()}]}
####################################################################################
######################### (▰˘◡˘▰) Functions (▰˘◡˘▰) ##########################
####################################################################################
@spec create(list(struct()), String.t()) :: :ok | error_return
def create(plugins, event) do
module = module_event_name(event)
escaped_plugins = Macro.escape(plugins)
Expand Down Expand Up @@ -78,11 +80,13 @@ defmodule MishkaInstaller.Event.ModuleStateCompiler do
{:error, [%{message: "Unexpected error", field: :event, action: :compile}]}
end

@spec purge_create(list(struct()), String.t()) :: :ok | error_return
def purge_create(plugins, event) do
purge(event)
create(plugins, event)
end

@spec purge(list(String.t()) | String.t()) :: :ok
def purge(events) when is_list(events) do
Enum.each(events, &purge(&1))
:ok
Expand All @@ -98,6 +102,7 @@ defmodule MishkaInstaller.Event.ModuleStateCompiler do
####################################################################################
########################## (▰˘◡˘▰) Helper (▰˘◡˘▰) ############################
####################################################################################
@spec module_event_name(String.t()) :: module()
def module_event_name(event) do
event
|> String.trim()
Expand All @@ -108,25 +113,30 @@ defmodule MishkaInstaller.Event.ModuleStateCompiler do
|> then(&Module.concat([&1]))
end

@spec initialize?(String.t()) :: boolean()
def initialize?(event), do: module_event_name(event).initialize?

@spec rescue_initialize?(String.t()) :: boolean()
def rescue_initialize?(event) do
module = module_event_name(event)
module.initialize?
rescue
_ -> false
end

@spec compile_initialize?(String.t()) :: boolean()
def compile_initialize?(event) do
module = module_event_name(event)
Code.ensure_loaded?(module)
end

@spec safe_initialize?(String.t()) :: boolean()
def safe_initialize?(event) do
module = module_event_name(event)
function_exported?(module, :initialize?, 0)
end

@spec perform(list(), {:reply, any()} | {:reply, :halt, any()}) :: any()
def perform([], {:reply, state}), do: state

def perform(_plugins, {:reply, :halt, state}), do: state
Expand Down

0 comments on commit 76fa2d0

Please sign in to comment.