FlipFlop
Provide an easy mechanism for turning features on or off, either with dates or just a boolean value.
Install
Add the following to your Gemfile
gem 'flip-flop'
and run bundler to install
$ bundle
or
$ gem install flip-flop
Configure
in config/initializers/flip-flop.rb
require 'flip-flop/adapters/yaml'
FlipFlop.configure do |config|
config[:adapter] = FlipFlop::Adapters::YAML
config[:yaml_path] = 'config/flip_flop.yml'
endand add a YAML file: config/flip_flop.yml
for example:
---
:after_date_example:
:type: :after_date
:value: 2016-01-01
:until_date_example:
:type: :until_date
:value: 2020-01-01
:percentage_of_time_example:
:type: :percentage_of_time
:value: 50
:date_range_example:
:type: :date_range
:value: !ruby/range
begin: 2016-01-01
end: 2016-02-01
excl: false
:time_range_example:
:type: :time_range
:value: !ruby/range
begin: 2016-01-01 01:21:15 UTC
end: 2016-01-02 01:24:15 UTC
excl: true # include last second in this case (false is .. notation while true is ... when exec by ruby)
:rails_env_example:
:type: :rails_env
:value: :production
:another_rails_env_example:
:type: :rails_env
:value:
- :production
- :test
:group_example:
:type: :group
:value: :group_nameExample Usage
Once FlipFlop has been configured, (see above section) you can start checking if features are enabled or not.
In a Rails View:
if feature_enabled? :my_cool_feature
puts 'Cool'
else
puts ''
endIn a Rails Controller:
class SomeController < ApplicationController
include FlipFlop::ViewHelpers
def my_action
load_cool_stuff if feature_enabled? :cool_stuff
end
endWithout Rails:
if FlipFlop.feature_enabled? :some_feature
puts 'something'
else
puts 'something else'
endGates
-
boolean— enable or disable a feature -
after_date— enable a feature after the date has passed -
until_date— enable a feature until the date has passed -
date_range— enable a feature for the duration of the date range -
after_time— enable a feature after a time has passed -
until_time— enable a feature until a time has passed -
time_range— enable a feature for the duration of a time range -
percentage_of_time— enable a feature for a given percentage of checks -
rails_env— enable a feature for a specified Rails environment -
group— enable a feature for a specified group of users (needs additional configuration)
Groups
To use the group adapter you need to register a group. For Ruby on Rails this can be done in an initializer. Groups are reusable, many features can be tied to a single group.
FlipFlop::Group.register(:administrators) do |user|
user.is_admin?
endSetup your feature definition. Here is an example using the YAML adapter:
---
:awesome_admin_only_feature
:type: :group
:value: :administratorsWhen checking if the feature is enabled for that group you can provide the actor (user).
FlipFlop.feature_enabled? :awesome_admin_only_feature, userAdapters
Memory
This adapter is primarily used for testing, all configuration is stored in a hash in memory.
YAML
The FlipFlop::Adapters::YAML adapter is uses a YAML config file, to store info
about enabled and disabled features. The YAML file is parsed once when the application is
loaded. It's quick and easy to use, but it requres a deployment if a feature needs
to be quickly flipped.
ActiveRecord
Allows feature settings to be stored in a database table. Because of typecasting and additional garbage collection this will not be as fast as the YAML adapter. Futher efforts will be made to makeing The ActiveRecord adapter more performant.
Extending FlipFlop
Adding Custom Gates
You can easily add custom gates by adding methods to the FlipFlop::Gates module,
see the below example.
module FlipFlop::Gates
def always_on(value)
true
end
endAdding Custom Adapters
Custom adapters can be added by creating a new adapter class, and then setting the adapter when configuring the FlipFlop. See the "Configure" for an example.
Contributing to FlipFlop
- fork
- branch
- commit
- push
- pull request