FeatureFlippers (FeatureFlippers v1.0.1) View Source
Provides a mechanism to turn features on and off within your application.
Declare a module where you'll define the available FFs and just declare them all:
defmodule Flags do
use FeatureFlippers, otp_app: :my_app
feature_flipper :foo?
feature_flipper :bar?, expires: "2000-01-01"
feature_flipper :baz?, disabled: true
endEach feature_flipper/2 declaration defines a function returning boolean in
the containing module. So, in your code, use them as follows:
if Flags.foo? do
# new feature
else
# original code
endIt means that if you remove a feature flipper declaration, your code won't
compile and mix will also indicate where the missing feature flippers are
being used. This may be useful when decommissioning feature flippers.
You can turn them on/off in compile time setting them in your
config/config.exs (or in runtime through config/runtime.exs):
config :my_app, Flags,
foo?: true
bar?: false
baz: trueYou can also set them later by updating the application environment:
Application.get_env(:my_app, Flags)
|> Keyword.update(:bar?, true, fn _ -> true end)
|> (&Application.put_env(:my_app, Flags, &1)).()While running unit tests, you might want to enable them all:
Flags.all()
|> Enum.map(fn key -> {key, true} end)
|> (&Application.put_env(:my_app, Flags, &1)).()At any moment, you can discover which feature flippers have expired calling
expired/0.
If you want to use a different storage for your flags instead of
Application environment, you can specify the :callback option, such as:
defmodule Flags do
use FeatureFlippers, callback: &Flags.on?/1
def on?(flag) do
# access `flag` or return `nil` if not found.
end
end
Link to this section Summary
Functions
Defines a feature flipper.
Link to this section Functions
Defines a feature flipper.
The name must end with ?.
Examples
feature_flipper :foo?
feature_flipper :bar?, expires: ~D[2000-01-01]
feature_flipper :baz?, disabled: trueOptions
feature_flipper/2 accepts the following options:
:expires- a string inYYYY-MM-DDformat representing the date when it should expire or aDatestruct.FeatureFlippersdoes not alter any feature flipper flag in execution when they expire; this works as informational indication to developers when the respective flag should be decommissioned. The functionexpired/0will also show the expired flag names.:disabled- a boolean indicating if the feature flipper should be forcefully disabled. This is defined in compile time, so if a feature flipper hasdisabled: true, it means that no application configuration will be read; the developer should reenable it and recompile the code if they need to turn it on again. It can be used when new features are not prepared for production yet, therefore they should never have the capability to be turned on.:default- if the feature flipper flag is not present in theApplication.get_env/3, assume this boolean as default. If not set,falseis the default.