Skip to content

Instantly share code, notes, and snippets.

@gabrielelana
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gabrielelana/de67e9ef59c21dbb9766 to your computer and use it in GitHub Desktop.
Save gabrielelana/de67e9ef59c21dbb9766 to your computer and use it in GitHub Desktop.
Remove things in production
defmodule Paco.Collector do
@env String.to_atom(System.get_env("MIX_ENV") || "dev")
defmacro notify_loaded(text, state), do: call(@env, :do_notify_loaded, [text, state])
defmacro notify_started(parser, state), do: call(@env, :do_notify_started, [parser, state])
defmacro notify_ended(result, state), do: call(@env, :do_notify_ended, [result, state])
defp call(:prod, _, _), do: :ok
defp call(_, name, args) do
quote do
unquote(name)(unquote_splicing(args))
end
end
def do_notify_loaded(_text, %Paco.State{collector: nil}), do: nil
def do_notify_loaded(_text, %Paco.State{silent: true}), do: nil
def do_notify_loaded(text, %Paco.State{collector: collector}) do
GenEvent.notify(collector, {:loaded, text})
end
def do_notify_started(_parser, %Paco.State{collector: nil}), do: nil
def do_notify_started(_parser, %Paco.State{silent: true}), do: nil
def do_notify_started(parser, %Paco.State{collector: collector}) do
GenEvent.notify(collector, {:started, Paco.describe(parser)})
end
def do_notify_ended(result, %Paco.State{collector: nil}), do: result
def do_notify_ended(result, %Paco.State{silent: true}), do: result
def do_notify_ended(%Paco.Success{} = success, %Paco.State{collector: collector}) do
GenEvent.notify(collector, {:matched, success.from, success.to, success.at})
success
end
def do_notify_ended(%Paco.Failure{} = failure, %Paco.State{collector: collector}) do
GenEvent.notify(collector, {:failed, failure.at})
failure
end
end
@gabrielelana
Copy link
Author

Not sure this is a good solution. Also I need to keep do_notify_* public otherwise I have a warning when I compile in production saying that do_notify_* are not used

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment