featureflags¶ ↑
Simple implementation of the ‘Feature Flags’ pattern as a Ruby gem. Allows you to set defaults in a Hash of the form:
{ feature_name_1: true, feature_name_2: false, feature_with_variations: 'A' }
and override them with correspondingly-named environment variables. In the example above, you could enable the feature ‘feature_name_2’ with the environment variable ‘FEATURE_NAME_2’.
Usage¶ ↑
In your gemfile:
gem "ministryofjustice-featureflags"
If you’re using Rails:¶ ↑
You can set defaults during Rails initialization, usually with a dedicated file in config/initializers:
defaults = { some_feature: true, some_other_feature: false, another_feature: 'variant-1' # <- any non-falsey value will be treated as enabled } Features::FeatureFlags.init!(defaults)
Setting Flags With Environment Variables¶ ↑
You can also override these values by starting Rails with the correspondingly-named environment variable. For instance, some_other_feature would be mapped to ENV. The environment variables are checked _at runtime_ (i.e. whenever enabled? is called).
Checking The Flags¶ ↑
In your application code:
if FeatureFlags::Features.enabled?(:some_feature) # do something else # do something different
Groups of features:
# ALL if FeatureFlags::Features.all_enabled?(:some_feature, :some_other_feature) # ANY if FeatureFlags::Features.any_enabled?(:some_feature, :some_other_feature)
Non-boolean Flags¶ ↑
When checking enabled?, any non-false-y value will return true. This allows you to transparently store non-boolean values in flags, for instance in A/B testing:
Features::FeatureFlags.init!(my_feature_variant: 'variant-A') case Features::FeatureFlags.flag(:my_feature) when 'variant-A' # do something... when 'variant-B' # do something else... end
Contributing to featureflags¶ ↑
-
Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.
-
Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.
-
Fork the project.
-
Start a feature/bugfix branch.
-
Commit and push until you are happy with your contribution.
-
Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.
-
Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
Copyright¶ ↑
Copyright © 2015 Al Davidson. See LICENSE.txt for further details.