Usage Guide
Once Shapt
is one of his dependencies. You gonna need to create a feature toggle module:
defmodule TestModule do
use Shapt,
adapter: [
prod: {Shapt.Adapters.Env, []}
],
toggles: [
feature_x?: %{
force: true,
key: "MYAPP_FEATURE_X",
deadline: ~D[2019-12-31]
},
feature_y?: %{
deadline: ~D[2009-12-31]
}
]
end
This module is a worker that you need to add to your supervision tree. Shapt
will create the proper child_spec/1
and start_link/1
functions.
Options
adapter
: receives a keyword list of adapters by environment.toggles
: receives a keyword list of toggles to be configured.
Default configurations
If no adapter is provided Shapt gonna set by default, Shapt.Adapters.Env
as the
production adapter and Shapt.Adapters.DotEnv
as develompment and test adapter.
toggles
option has no default values.
Provided functions
Just by using
Shapt you gonna have some extra functions on your feature toggle
module:
key_name/0
For each key in the toggles
option keyword list you gonna have a fucntion that
represents that feature toggle. From the example above you would have in
TestModule
the functions feature_x?/0
and feature_y?/0
, those functions
always return only true
or false
.
toggle/2
Besides the key named functions you gonna have toggle/2
. It can be used to
evaluate the feature toggle and what to do when it's on or off. Some usage
examples:
# just returning values
TestModule.toggle(:feature_x?, on: FeatureX, off: OldFeature)
# applying functions that receive the same parameters
TestModule.toggle(:feature_x?, env: [params, options], on: &Feature.x/2, off: &Feature.old/2)
# applying functions with different parameters:
TestModule.toggle(:feature_x?, on: {ModuleX, :function, [paramsx]}, off: {Module, :fuction, [params]})
In those two last examples we use the apply
function, be aware of that when
using this feature. So make sure that the env
or the third element tuple is
always a list which the size is the same as the provided function arity.
expired?/1
It receives the name of a toggle and says if it's expired or not.
enabled?/1
It receives the name of a toggle and says if it's enabled or not.
expired_toggles/0
List all expired toggles
output_template/0
Return the binary representing the template file generated by the Adapter.
instance_name/0
By default instance_name/0
will always return nil
. This function is
overridable so when needed you can specified the behavior to determine which
instance we are running. For the default Adapters provided here this function
has no utility, but it gonna be useful for future Adapters.
Env Adapter
Shapt.Adapters.Env
will work directly with environment variables using the System.get_env/1
.
This is why the adapter doesn't receive any configuration.
DotEnv Adapter
Shapt.Adapters.DotEnv
works with a .env
file. If no option is provided it
gonna try to read a .env
file from the root of the mix project you're running.
To provide a file you can set {Shapt.Adapters.DotEnv, [file: "path/to/file.env"]}
on
your adapter configuration.
Testing
Shapt
is built having in mind that you probably gonna use
Mox
to write tests. By using Mox
you only
need mock enabled?/2
callback. You can have more info on Shapt.Adapter
behaviour docs.