Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/tdumitrescu/feature_guard
Lightweight Redis-based feature-flagging for Ruby apps
https://github.com/tdumitrescu/feature_guard
Last synced: about 2 months ago
JSON representation
Lightweight Redis-based feature-flagging for Ruby apps
- Host: GitHub
- URL: https://github.com/tdumitrescu/feature_guard
- Owner: tdumitrescu
- License: mit
- Created: 2013-10-25T16:30:59.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2022-09-21T22:09:22.000Z (over 2 years ago)
- Last Synced: 2024-10-17T21:43:18.059Z (2 months ago)
- Language: Ruby
- Size: 188 KB
- Stars: 43
- Watchers: 3
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://api.travis-ci.org/tdumitrescu/feature_guard.png)](https://travis-ci.org/tdumitrescu/feature_guard)
# FeatureGuard
Lightweight Redis-based feature-flagging for Ruby apps. Provides a simple syntax for enabling and disabling features, or gradually ramping up and down by enabling features for a percentage of total traffic.
## Installation
Add this line to your application's Gemfile:
gem 'feature_guard'
And then execute:
$ bundle
Or install it yourself as:
$ gem install feature_guard
## Usage
Check whether a feature is enabled globally:
```ruby
FeatureGuard.enabled? :my_feature
```Globally enable or disable a feature:
```ruby
FeatureGuard.enable :my_feature
FeatureGuard.disable :my_feature
FeatureGuard.toggle :my_feature
```Feature names can be strings or symbols. No data setup is necessary; any check for a feature which has never been enabled simply returns false.
For more fine-grained control, set a ramp-up value to decide which percentage of traffic should see the feature:
```ruby
FeatureGuard.set_ramp :my_feature, 30.5 # 30.5%
FeatureGuard.bump_ramp :my_feature, 12 # 30.5 + 12 = 42.5%
FeatureGuard.bump_ramp :my_feature # 42.5 + 10 = 52.5%FeatureGuard.ramp_val :my_feature # 52.5
````.set_ramp` sets the ramp-up value; `.bump_ramp` increments or decrements it by a given value (defaults to 10.0). Check the current ramp-up value with `.ramp_val`.
Check whether to show the feature at the current ramp-up value:
```ruby
FeatureGuard.allow? :my_feature, user_id
# true for 52.5% of user_id valuesFeatureGuard.allow? :my_feature
# true for 52.5% of checks (random)
```The optional second argument to`.allow?` can be of any type (e.g., user ID or name or even an object). It is hashed with the feature name to create a reproducible numeric value for checking whether to return true or false based on the current ramp-up value. With no second argument, `.allow?` uses a new random value on every call.
To retrieve information on all binary-enabled/disabled features, use `.all_flags`:
```ruby
FeatureGuard.enable :my_feature
FeatureGuard.enable :another_feature
FeatureGuard.toggle :my_feature
FeatureGuard.all_flags
# {"my_feature"=>false, "another_feature"=>true}
```To retrieve information on all ramp values, use `.all_ramps`:
```ruby
FeatureGuard.set_ramp :my_feature, 50
FeatureGuard.set_ramp :another_feature, 30
FeatureGuard.all_ramps
# {"my_feature"=>50.0, "another_feature"=>30.0}
```## Configuration
Optionally change the Redis client with:
```ruby
FeatureGuard.redis = my_redis_client
```Setting `FeatureGuard.redis` to `nil` will revert it to a new default instance (`Redis.new`).
## Running tests
$ bundle exec rspec
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Added some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request