From 49ae8fed654a34fe7b4188d3cf60ad5803c63ed8 Mon Sep 17 00:00:00 2001 From: Jasdeep Singh Date: Sun, 13 Aug 2017 17:19:57 -0400 Subject: [PATCH] implement flipper via singleton + updated readme docs --- README.md | 30 ++++++++++++++++++++++++- spec/flipper_spec.cr | 6 +++++ spec/spec_helper.cr | 52 +++++++++++++++++++++++++------------------- src/flipper.cr | 39 +++++++++++++++++++++++---------- 4 files changed, 92 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index 206b313..2e425f1 100644 --- a/README.md +++ b/README.md @@ -22,7 +22,35 @@ dependencies: require "flipper" ``` -TODO: Write usage instructions here +``` +Flipper.adapter = Kiwi::MemoryStore.new +Flipper.adapter = Kiwi::RedisStore.new(Redis.new) +``` + +You are free to use anyone of the supported adapeters/stores from [crystal-kiwi](https://github.com/greyblake/crystal-kiwi) + +```crystal +Flipper.enable(:feature_name) + +if Flipper.enabled?(:feature_name) + puts "Feature launched, Let's roll!" +else + puts "Feature not released yet." +end + +Flipper.disable(:search) + +if Flipper.disabled?(:search) + puts "Search is not available yet!" +end +``` + +## Roadmap + +- [x] Simple Logic Gate +- [ ] Groups +- [ ] Individual Users/Actors +- [ ] Percentage of Actors ## Contributing diff --git a/spec/flipper_spec.cr b/spec/flipper_spec.cr index 7c660ac..c73e05b 100644 --- a/spec/flipper_spec.cr +++ b/spec/flipper_spec.cr @@ -1,6 +1,12 @@ require "./spec_helper" describe Flipper do + it "attempting to use flipper without adapter raise error" do + expect_raises(Flipper::AdapterNotSet) do + Flipper.enable(:search) + end + end + it "works as expected with MemoryStore" do works_as_expected_with(Kiwi::MemoryStore.new) end diff --git a/spec/spec_helper.cr b/spec/spec_helper.cr index bed50e1..13b716c 100644 --- a/spec/spec_helper.cr +++ b/spec/spec_helper.cr @@ -11,49 +11,57 @@ require "kiwi/memcached_store" macro works_as_expected_with(store) adapter = {{store}} + Flipper.adapter = adapter describe Flipper do - context "#enable" do + + context ".adapter" do + + it "can be read back" do + Flipper.adapter.should_not be_nil + end + + it "should be Kiwi::Store" do + Flipper.adapter.should be_a(Kiwi::Store) + end + + end + + context ".enable" do it "should enable a feature" do - flipper = Flipper.new(adapter) - flipper.enable(:search).should be_true + Flipper.enable(:search).should be_true end end - context "#disable" do + context ".disable" do it "should disable a feature" do - flipper = Flipper.new(adapter) - flipper.disable(:search).should be_true + Flipper.disable(:search).should be_true end end - context "#enabled?" do + context ".enabled?" do it "should check if a feature is enabled?" do - flipper = Flipper.new(adapter) - flipper.enable(:search) - flipper.enabled?(:search).should be_true - flipper.disabled?(:search).should be_false + Flipper.enable(:search) + Flipper.enabled?(:search).should be_true + Flipper.disabled?(:search).should be_false end it "should return false for a non-existent feature" do - flipper = Flipper.new(adapter) - flipper.enabled?(:non_existent_feature).should be_false - flipper.disabled?(:non_existent_feature).should be_true + Flipper.enabled?(:non_existent_feature).should be_false + Flipper.disabled?(:non_existent_feature).should be_true end end - context "#disabled?" do + context ".disabled?" do it "should check if a feature is disabled?" do - flipper = Flipper.new(adapter) - flipper.disable(:search) - flipper.enabled?(:search).should be_false - flipper.disabled?(:search).should be_true + Flipper.disable(:search) + Flipper.enabled?(:search).should be_false + Flipper.disabled?(:search).should be_true end it "should return true for a non-existent feature" do - flipper = Flipper.new(adapter) - flipper.enabled?(:non_existent_feature).should be_false - flipper.disabled?(:non_existent_feature).should be_true + Flipper.enabled?(:non_existent_feature).should be_false + Flipper.disabled?(:non_existent_feature).should be_true end end end diff --git a/src/flipper.cr b/src/flipper.cr index 4f9a337..70dec3e 100644 --- a/src/flipper.cr +++ b/src/flipper.cr @@ -1,27 +1,42 @@ require "./flipper/*" class Flipper - def initialize(@adapter : Kiwi::Store) + class AdapterNotSet < Exception end - def enable(feature) - @adapter.set(feature.to_s, "true") == "true" + def self.adapter=(adapter : Kiwi::Store) + @@adapter = adapter end - def disable(feature) - @adapter.set(feature.to_s, "false") == "false" + def self.adapter + raise Flipper::AdapterNotSet.new unless @@adapter + @@adapter end - def enabled?(feature) - stored_value = @adapter.get(feature.to_s) - if stored_value.nil? || stored_value == "false" - false - elsif stored_value == "true" - true + def self.enable(feature) + adapter.try do |adapter| + adapter.set(feature.to_s, "true") == "true" end end - def disabled?(feature) + def self.disable(feature) + adapter.try do |adapter| + adapter.set(feature.to_s, "false") == "false" + end + end + + def self.enabled?(feature) + adapter.try do |adapter| + stored_value = adapter.get(feature.to_s) + if stored_value.nil? || stored_value == "false" + false + elsif stored_value == "true" + true + end + end + end + + def self.disabled?(feature) !enabled?(feature) end end