Simple Feature Flipper in Ruby
require 'feature'
# feature = Feature.new(:new_year_landing_page)
# feature.activate(:company1)
if feature.activated?(:company1)
show_new_year_landing_page
else
show_regular_landing_page
end
require 'spec_helper'
require 'mock_redis'
describe Feature do
let(:redis) { MockRedis.new }
before(:each) do
Feature.redis = redis
end
describe '#activate' do
it 'activates feature for given company' do
feature_name = :nice_new_feature
feature = Feature.new(feature_name)
feature.activate(:company1)
redis_namespace = "sys:ff"
redis_key = "#{redis_namespace}:nice_new_feature:company1"
expect(redis.get(redis_key)).to eq('true')
end
end
describe '#activated?' do
context 'feature activated for the company' do
it 'returns true' do
feature_name = :nice_new_feature
feature = Feature.new(feature_name)
feature.activate(:company1)
expect(feature.activated?(:company1)).to eq(true)
end
end
context "feature isn't activated for the company" do
it 'returns false' do
feature_name = :nice_new_feature
feature = Feature.new(feature_name)
expect(feature.activated?(:company1)).to eq(false)
end
end
end
end
require 'mock_redis'
class Feature
class << self
attr_accessor :redis
end
def initialize(name)
@name = name
end
def activate(company_name)
redis_key = ['sys', 'ff', @name, company_name].join(':')
self.class.redis.set(redis_key, true)
end
def activated?(company_name)
redis_key = ['sys', 'ff', @name, company_name].join(':')
self.class.redis.get(redis_key) == 'true'
end
end