Feature flags that can be overwritten on the fly. It could be per instance or per environment. Depending on how you set your key, either dynamically per instance or constant per environment.
It uses Redis to store the information, and the size should be very small, depending on how many flags you need to track.
Add this line to your application's Gemfile:
gem 'fflags'
And then execute:
$ bundle
Or install it yourself as:
$ gem install fflags
To configure the FFlags,
FFlags.config { |config .. }
Config | Description | Default |
---|---|---|
key | Key to store the flags, in Redis, it would look something like, { key: <flags as a Hash> } |
code3.io |
flags | Flags to be tracked as a Hash, and value as a boolean) | {} |
redis_url | Redis url, where the flags will be stored | redis://127.0.0.1:6379 |
debug | For debugging, NOT BEING USED RIGHT NOW | false |
- If you want the flags to be unique per instance, you can use hostname of the instance as such:
FFlags.config do |config|
config.key = Socket.gethostname
config.flags = { new_view: true }
end
- If you want the flags to be unique per environment, you can use the key as such:
FFlags.config do |config|
config.key = Rails.environment,
config.flags = { new_view: true }
end
Then in the code,
# Verify if the flag is enabled
if FFlags.enabled?(:new_view)
# Do something
end
# or
if FFlags.new_view?
# Do something
end
# Overwrite flag
FFlags.toggle(:new_view)
# or
FFlags.set(:new_view, false)
# To reset, and get back to the default settings.
FFlags.reset
# To read all flags
FFlags.all
- Add the gem in Gemfile.
# Gemfile.rb
gem 'fflags'
- Add the initializer.
# config/initializer/fflags.rb
FFlags.config do |config|
config.flags = { new_feature: true }
..
end
- Use the feature anywhere in your code.
if FFlags.new_feature?
puts 'Enabled'
end
Sometimes you need to set a set of flags in 1 go. Ex. Staging to have an exact same flag configuration as in Production. You can use template for this.
Ex.
In the config,
FFlags.config do |config|
config.flags = { feature1: true, feature2: true, feature3: true }
config.templates = { production: { feature1: false, feature2: false, feature3: false } }
end
Then in your code,
FFlags.set_as_template(:production)
# To view all the templates
FFlags.templates
Sometimes you need to test flag with different value in your test. You can set block for that.
Ex. in rspec
describe "#eat_tofu" do
context 'new feature enabled' do
FFlags.set(new_feature: true) do
# Do something
end
end
context 'new feature disabled' do
FFlags.set(new_feature: false) do
# Do something
end
end
end
After checking out the repo, run bundle install
to install dependencies. Then, run rspec
to run the tests.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/faizalzakaria/fflags.
The gem is available as open source under the terms of the MIT License.