Bang simply adds dynamic bang(!) functions to your existing module functions with after-callback.
If available in Hex, the package can be installed as:
- Add
bang
to your list of dependencies inmix.exs
:
def deps do
[{:bang, "~> 0.1.0"}]
end
- Ensure
bang
is started before your application:
def application do
[applications: [:bang]]
end
Bang uses accumulated approach by definition. So, you can define several bangs inside your module and can use different callback for each @bang function.
defmodule SampleModule do
use Bang
# @bang {[list_of_func_name_arg_count_tuples], {CallbackModule, :callback_fn}}
@bang {[welcome: 1, welcome: 2], {SampleModule, :titleize}}
def welcome(name), do: "Welcome #{name}!"
def welcome(prefix, name), do: "Welcome #{prefix}. #{name}!"
@bang {[good_bye: 1, good_bye: 2], {SampleModule, :titleize}}
def good_bye(name), do: "Good bye #{name}!"
def good_bye(prefix, name), do: "Good bye #{prefix}. #{name}!"
def titleize(str) do
str
|> String.split(" ")
|> Enum.map(fn(word) -> String.capitalize(word) end)
|> Enum.join(" ")
end
end
SampleModule.welcome("john doe") |> SampleModule.titleize == SampleModule.welcome!("john doe")
The code above will add welcome!: 1, welcome!:2, good_bye!: 1, good_bye!: 2
functions to the SampleModule
Open your iex console with iex -S mix
command and run for
iex> SomeModule.__info__(:functions)
# will result with list of tuples
And then add bang function as usual using the output list.
defmodule SomeModule
use Bang
...
@bang {[list_of_tuples], {SomeOtherModule, :func_name}}
...
end
-
Fork the project
-
Make your improvements and write your tests.
-
Make a pull request.