-
-
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.
Add Strict adapter to ensure feature exists on get
- Loading branch information
Showing
3 changed files
with
73 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
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,44 @@ | ||
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 | ||
|
||
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: ->(_) { }, | ||
} | ||
|
||
DEFAULT_NOT_FOUND = | ||
|
||
def_delegators :@adapter, :features, :get, :get_multi, :get_all, :add, :remove, :clear, :enable, :disable | ||
|
||
def initialize(adapter, name: :strict, handler: :raise) | ||
@name = name | ||
@adapter = adapter | ||
@handler = handler.is_a?(Symbol) ? HANDLERS.fetch(handler) : handler | ||
end | ||
|
||
def get(feature) | ||
assert_feature_exists(feature) | ||
@adapter.get(feature) | ||
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,28 @@ | ||
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, handler: :noop) } | ||
end | ||
|
||
context "handler: :raise" do | ||
subject { described_class.new(Flipper::Adapters::Memory.new, handler: :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 | ||
end | ||
|
||
context "handler: :warn" do | ||
subject { described_class.new(Flipper::Adapters::Memory.new, handler: :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 | ||
end | ||
end |