Last active
August 29, 2015 14:21
-
-
Save gabrielelana/de67e9ef59c21dbb9766 to your computer and use it in GitHub Desktop.
Remove things in production
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 thatdo_notify_*
are not used