https://github.com/wojtha/simple_flag
Simple but powerful feature flag implementation in 90 LOC
https://github.com/wojtha/simple_flag
feature-flags ruby ruby-gem ruby-library
Last synced: 4 months ago
JSON representation
Simple but powerful feature flag implementation in 90 LOC
- Host: GitHub
- URL: https://github.com/wojtha/simple_flag
- Owner: wojtha
- License: mit
- Created: 2020-04-28T11:21:34.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2023-11-01T22:07:10.000Z (over 1 year ago)
- Last Synced: 2025-03-03T08:16:58.211Z (4 months ago)
- Topics: feature-flags, ruby, ruby-gem, ruby-library
- Language: Ruby
- Size: 19.5 KB
- Stars: 1
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# SimpleFlag
Simple but powerful feature flag implementation in 90 LOC.
## Installation
Add this line to your application's Gemfile:
```ruby
gem 'simple_flag'
```And then execute:
$ bundle install
Or install it yourself as:
$ gem install simple_flag
## Usage
### Configuration
```ruby
FEATURE = SimpleFlag.new do |feature|
feature.define(:new_user_profile) do |user_id:|
Admin.where(user_id: user_id).exists?
end
feature.define(:third_party_analytics) do
not Rails.env.production?
end
end
```### Usage in app code
```ruby
class ProfilesController < ApplicationController
def show
FEATURE.with(:new_user_profile, user_id: current_user.id) do
return render :new_user_profile, locals: { user: NewUserProfilePresenterV2.new(current_user) }
end
render :show, locals: { user: UserProfilePresenterV1.new(current_user) }
end
end
```### Testing with before...after
```ruby
describe "User profiles" do
after { FEATURE.reset_all_overrides }context "with new_user_profile feature enabled" do
before { FEATURE.override(:new_user_profile, true) }
it "renders new profile"
endcontext "with new_user_profile feature disabled" do
before { FEATURE.override(:new_user_profile, false) }
it "renders old profile"
end
end
```### Testing with inline block
```ruby
it "shows new user profile" do
FEATURE.override_with(:new_user_profile, true) do
expect( FEATURE.active?(:new_user_profile) ).to be_truthy
end
expect( FEATURE.active?(:new_user_profile) ).to be_falsy
end
```## Development
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can
also run `bin/console` for an interactive prompt that will allow you to experiment.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](https://rubygems.org).## Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/wojtha/simple_flag.