-
-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #760 from flippercloud/strict
Add Strict adapter to ensure feature exists on get
- Loading branch information
Showing
4 changed files
with
128 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'bundler/setup' | ||
require 'flipper' | ||
|
||
adapter = Flipper::Adapters::Strict.new(Flipper::Adapters::Memory.new) | ||
flipper = Flipper.new(adapter) | ||
|
||
begin | ||
puts "Checking :unknown_feature, which should raise an error." | ||
flipper.enabled?(:unknown_feature) | ||
warn "An error was not raised, but should have been" | ||
exit 1 | ||
rescue Flipper::Adapters::Strict::NotFound => exception | ||
puts "Ok, the exepcted error was raised: #{exception.message}" | ||
end | ||
|
||
puts "Flipper.add(:new_feature)" | ||
flipper.add(:new_feature) | ||
puts "Flipper.enabled?(:new_feature) => #{flipper.enabled?(:new_feature)}" |
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,47 @@ | ||
module Flipper | ||
module Adapters | ||
# An adapter that ensures a feature exists before checking it. | ||
class Strict | ||
extend Forwardable | ||
include ::Flipper::Adapter | ||
attr_reader :name, :adapter, :handler | ||
|
||
class NotFound < ::Flipper::Error | ||
def initialize(name) | ||
super "Could not find feature #{name.inspect}. Call `Flipper.add(#{name.inspect})` to create it." | ||
end | ||
end | ||
|
||
HANDLERS = { | ||
raise: ->(feature) { raise NotFound.new(feature.key) }, | ||
warn: ->(feature) { warn NotFound.new(feature.key).message }, | ||
noop: ->(_) { }, | ||
} | ||
|
||
def_delegators :@adapter, :features, :get_all, :add, :remove, :clear, :enable, :disable | ||
|
||
def initialize(adapter, handler = nil, &block) | ||
@name = :strict | ||
@adapter = adapter | ||
@handler = block || HANDLERS.fetch(handler) | ||
end | ||
|
||
def get(feature) | ||
assert_feature_exists(feature) | ||
@adapter.get(feature) | ||
end | ||
|
||
def get_multi(features) | ||
features.each { |feature| assert_feature_exists(feature) } | ||
@adapter.get_multi(features) | ||
end | ||
|
||
private | ||
|
||
def assert_feature_exists(feature) | ||
@handler.call(feature) unless @adapter.features.include?(feature.key) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
RSpec.describe Flipper::Adapters::Strict do | ||
let(:flipper) { Flipper.new(subject) } | ||
let(:feature) { flipper[:unknown] } | ||
|
||
it_should_behave_like 'a flipper adapter' do | ||
subject { described_class.new(Flipper::Adapters::Memory.new, :noop) } | ||
end | ||
|
||
context "handler = :raise" do | ||
subject { described_class.new(Flipper::Adapters::Memory.new, :raise) } | ||
|
||
context "#get" do | ||
it "raises an error for unknown feature" do | ||
expect { subject.get(feature) }.to raise_error(Flipper::Adapters::Strict::NotFound) | ||
end | ||
end | ||
|
||
context "#get_multi" do | ||
it "raises an error for unknown feature" do | ||
expect { subject.get_multi([feature]) }.to raise_error(Flipper::Adapters::Strict::NotFound) | ||
end | ||
end | ||
end | ||
|
||
context "handler = :warn" do | ||
subject { described_class.new(Flipper::Adapters::Memory.new, :warn) } | ||
|
||
context "#get" do | ||
it "raises an error for unknown feature" do | ||
expect(silence { subject.get(feature) }).to match(/Could not find feature "unknown"/) | ||
end | ||
end | ||
|
||
context "#get_multi" do | ||
it "raises an error for unknown feature" do | ||
expect(silence { subject.get_multi([feature]) }).to match(/Could not find feature "unknown"/) | ||
end | ||
end | ||
end | ||
|
||
context "handler = Block" do | ||
let(:unknown_features) { [] } | ||
subject do | ||
described_class.new(Flipper::Adapters::Memory.new) { |feature| unknown_features << feature.key} | ||
end | ||
|
||
|
||
context "#get" do | ||
it "raises an error for unknown feature" do | ||
subject.get(feature) | ||
expect(unknown_features).to eq(["unknown"]) | ||
end | ||
end | ||
|
||
context "#get_multi" do | ||
it "raises an error for unknown feature" do | ||
subject.get_multi([flipper[:foo], flipper[:bar]]) | ||
expect(unknown_features).to eq(["foo", "bar"]) | ||
end | ||
end | ||
end | ||
end |