Skip to content

Latest commit

 

History

History
78 lines (53 loc) · 2.14 KB

USAGE.md

File metadata and controls

78 lines (53 loc) · 2.14 KB

Usage Guide

Once Shapt is one of his dependencies. You gonna need to create a feature toggle module:

defmodule TestModule do
  use Shapt,
    adapter: {Shapt.Adapters.Env, []},
    toggles: [
      feature_x?: %{
        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. All the state of the toggles are kept inside an ets table.

Options

  • adapter: receives a keyword list of adapters by environment.
  • toggles: receives a keyword list of toggles to be configured.

Provided functions

Just by using Shapt you gonna have some extra functions on your feature toggle module:

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 and params:
TestModule.toggle(:feature_x?, on: {&Feature.x/2, params_x}, off: {&Feature.old/2, params_old})

# applying module, function_name and params
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 params provided are 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

template/0

Return the binary representing the template file generated by the Adapter.

reload/0

Reloads the ets table with the current state of toggles according to the adapter options.

Env Adapter

Shapt.Adapters.Env will work directly with environment variables using the System.get_env/1 or loading it from a configuration file.