Created
January 5, 2011 07:46
-
-
Save vanpelt/766034 to your computer and use it in GitHub Desktop.
Simple ruby module to make rolling out features easy...
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module Features | |
| def self.allowed?(feature, user) | |
| case redis.get(key(feature)) | |
| when "admin": user.admin | |
| when "diy": user.admin || user.diy | |
| when "enterprise": user.admin || !user.diy | |
| when "all": true | |
| when /^\d+$/: user.admin || user.id % state.to_i == 0 | |
| else | |
| false | |
| end | |
| end | |
| def self.rollout(feature, user, &block) | |
| yield if allowed?(feature, user) | |
| end | |
| #Dirty dirty magic... | |
| class << self | |
| [:admin, :diy, :enterprise, :all].each do |state| | |
| define_method(state.to_s+"!") do |feature| | |
| redis.set(key(feature), state) | |
| end | |
| end | |
| end | |
| def self.percentage!(feature, percentage) | |
| raise "percentage must be between 0 & 100" unless (0..100).include?(percentage.to_i) | |
| redis.set(key(feature), percentage.to_i) | |
| end | |
| def self.disable!(feature) | |
| redis.del(key(feature)) | |
| end | |
| def self.redis | |
| $redis | |
| end | |
| def self.key(feature) | |
| "feature:#{feature}" | |
| end | |
| module Helpers | |
| def allowed?(feature) | |
| Features.allowed?(feature, session[:user]) | |
| end | |
| def rollout(feature, &block) | |
| Features.rollout(feature, session[:user], &block) | |
| end | |
| end | |
| end |
It'd be great to avoid too frequent roundtrips to redis. You can have a redis-configurable TTL for this class. It'll be totally fine to hit redis once every minute or five. If it's configurable, you can crank it down to be 1 second if you want to really play with the feature.
Can we make it more nuclear? If you disable JobHistory, Rollout can patch JobHistory so that all methods return a FeatureDisabled object. This way you avoid littering your code with conditional statements. (thinking aloud)
I'm sure various e-mail senders will use rollout very quickly :)
hrmm. +1 pimp.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Once we are totally comfortable with the feature we remove the
rolloutlines.