Skip to content

Commit

Permalink
implement flipper via singleton + updated readme docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jasdeepsingh committed Aug 13, 2017
1 parent 7d1f646 commit 49ae8fe
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 35 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions spec/flipper_spec.cr
Original file line number Diff line number Diff line change
@@ -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
Expand Down
52 changes: 30 additions & 22 deletions spec/spec_helper.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 27 additions & 12 deletions src/flipper.cr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 49ae8fe

Please sign in to comment.