Feature is a battle-tested feature toggle library for ruby.
The feature toggle functionality has to be configured by feature repositories. A feature repository simply provides lists of active features (symbols!). Unknown features are assumed deactive. With this approach Feature is higly configurable and not bound to a specific kind of configuration.
gem install feature
-
Setup Feature
-
Create a repository (see examples below)
-
set repository to Feature
Feature.set_repository(your_repository)
-
-
Use Feature in your production code
Feature.active(:feature_name) # => true/false Feature.deactive?(:feature_name) # => true/false Feature.with(:feature_name) do # code end Feature.without(:feature_name) do # code end
-
Use Feature in your test code (for reliable testing of feature depending code)
require 'feature/testing' Feature.run_with_activated(:feature_name) do # your test code end Feature.run_with_deactivated(:feature_name) do # your test code end
# setup code
require 'feature'
repo = Feature::Repository::SimpleRepository.new
repo.add_active_feature :be_nice
Feature.set_repository repo
# production code
Feature.active?(:be_nice)
# => true
Feature.with(:be_nice) do
puts "you can read this"
end
# File: Gemfile
gem 'feature' # Or with version specifier, e.g. '~> 0.6.0'
# File: config/feature.yml
features:
an_active_feature: true
an_inactive_feature: false
# File: config/initializers/feature.rb
repo = Feature::Repository::YamlRepository.new("#{Rails.root}/config/feature.yml")
Feature.set_repository repo
# File: app/views/example/index.html.erb
<% if Feature.active?(:an_active_feature) %>
<%# Feature implementation goes here %>
<% end %>