Create a New Adapter
If you would like to make your own adapter, there are shared adapter specs (RSpec) and tests (MiniTest) that you can use to verify that you have everything working correctly.
API
The basic API for an adapter is this:
-
features
- Get the set of known features. -
add(feature)
- Add a feature to the set of known features. -
remove(feature)
- Remove a feature from the set of known features. -
clear(feature)
- Clear all gate values for a feature. -
get(feature)
- Get all gate values for a feature. -
enable(feature, gate, thing)
- Enable a gate for a thing. -
disable(feature, gate, thing)
- Disable a gate for a thing. -
get_multi(features)
- Get all gate values for several features at once. Implementation is optional. If none provided, default implementation performs N+1get
calls where N is the number of elements in the features parameter. -
get_all
- Get all gate values for all features at once. Implementation is optional. If none provided, default implementation performs two calls, one tofeatures
to get the names of all features and one toget_multi
with the feature names from the first call.
A good place to start when creating your own adapter is to copy one of the officially supported adapters and replace the client specific code with whatever client you are attempting to adapt.
MiniTest
Here is what an in-memory adapter MiniTest looks like:
test/adapters/memory_test.rb
require 'test_helper'
class MemoryTest < MiniTest::Test
prepend SharedAdapterTests
def setup
# Any code here will run before each test
@adapter = Flipper::Adapters::Memory.new
end
def teardown
# Any code here will run after each test
end
end
- Create a file under
test/adapters
that inherits fromMiniTest::Test
. -
prepend SharedAdapterTests
. - Initialize an instance variable
@adapter
referencing an instance of the adapter. - Add any code to run before each test in a
setup
method and any code to run after each test in ateardown
method.
RSpec
For example, here is what the in-memory adapter spec looks like:
spec/flipper/adapters/memory_spec.rb
require 'helper'
# The shared specs are included with the flipper gem so you can use them in
# separate adapter specific gems.
require 'flipper/spec/shared_adapter_specs'
describe Flipper::Adapters::Memory do
# an instance of the new adapter you are trying to create
subject { described_class.new }
# include the shared specs that the subject must pass
it_should_behave_like 'a flipper adapter'
end
Tip: Set fail_fast = true
in your RSpec configuration as that will just give you one failure at a time to work through. It is also handy to have the shared adapter spec file open.
Get audit history, rollbacks, advanced permissions, analytics, and all of your projects in one place.
You can choose from several tiers to sponsor Flipper on GitHub and get some great benefits!