forked from voormedia/flipflop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f79ffd
commit b7c85e6
Showing
9 changed files
with
148 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
module Flipflop | ||
module Strategies | ||
class RedisStrategy < AbstractStrategy | ||
class << self | ||
def default_description | ||
"Stores features in Redis. Applies to all users." | ||
end | ||
end | ||
|
||
def initialize(**options) | ||
@client = options.delete(:client) || ::Redis.new | ||
@prefix = options.delete(:prefix).to_s.freeze | ||
super(**options) | ||
end | ||
|
||
def switchable? | ||
true | ||
end | ||
|
||
def enabled?(feature) | ||
redis_value = @client.get(redis_key(feature)) | ||
return if redis_value.nil? | ||
redis_value === "1" | ||
end | ||
|
||
def switch!(feature, enabled) | ||
@client.set(redis_key(feature), enabled ? "1" : "0") | ||
end | ||
|
||
def clear!(feature) | ||
@client.del(redis_key(feature)) | ||
end | ||
|
||
protected | ||
|
||
def redis_key(feature) | ||
@prefix + feature.to_s | ||
end | ||
end | ||
end | ||
end |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
require File.expand_path("../../../test_helper", __FILE__) | ||
|
||
require "fakeredis" | ||
|
||
describe Flipflop::Strategies::RedisStrategy do | ||
before do | ||
Redis.new.flushall | ||
end | ||
|
||
describe "with defaults" do | ||
subject do | ||
Flipflop::Strategies::RedisStrategy.new.freeze | ||
end | ||
|
||
it "should have default name" do | ||
assert_equal "redis", subject.name | ||
end | ||
|
||
it "should have default description" do | ||
assert_equal "Stores features in Redis. Applies to all users.", | ||
subject.description | ||
end | ||
|
||
it "should be switchable" do | ||
assert_equal true, subject.switchable? | ||
end | ||
|
||
it "should have unique key" do | ||
assert_match /^\w+$/, subject.key | ||
end | ||
|
||
describe "with enabled feature" do | ||
before do | ||
Redis.new.set("one", 1) | ||
end | ||
|
||
it "should have feature enabled" do | ||
assert_equal true, subject.enabled?(:one) | ||
end | ||
|
||
it "should be able to switch feature off" do | ||
subject.switch!(:one, false) | ||
assert_equal false, subject.enabled?(:one) | ||
end | ||
|
||
it "should be able to clear feature" do | ||
subject.clear!(:one) | ||
assert_nil subject.enabled?(:one) | ||
end | ||
end | ||
|
||
describe "with disabled feature" do | ||
before do | ||
Redis.new.set("two", 0) | ||
end | ||
|
||
it "should not have feature enabled" do | ||
assert_equal false, subject.enabled?(:two) | ||
end | ||
|
||
it "should be able to switch feature on" do | ||
subject.switch!(:two, true) | ||
assert_equal true, subject.enabled?(:two) | ||
end | ||
|
||
it "should be able to clear feature" do | ||
subject.clear!(:two) | ||
assert_nil subject.enabled?(:two) | ||
end | ||
end | ||
|
||
describe "with unsaved feature" do | ||
it "should not know feature" do | ||
assert_nil subject.enabled?(:three) | ||
end | ||
|
||
it "should be able to switch feature on" do | ||
subject.switch!(:three, true) | ||
assert_equal true, subject.enabled?(:three) | ||
end | ||
end | ||
end | ||
|
||
describe "with options" do | ||
subject do | ||
Flipflop::Strategies::RedisStrategy.new( | ||
client: Redis.new(db: 1), | ||
prefix: "my_feature:", | ||
).freeze | ||
end | ||
|
||
it "should use prefix and database to resolve parameters" do | ||
Redis.new(db: 1).set("my_feature:one", 1) | ||
assert_equal true, subject.enabled?(:one) | ||
end | ||
end | ||
end |