Permalink
Browse files

Added clear and remove to adapter API.

  • Loading branch information...
jnunemaker committed Feb 20, 2013
1 parent 8d8c763 commit 58fbf8413221dfc4fcd5272ea737458e1e95e2d1
@@ -87,6 +87,30 @@ def add(feature)
payload[:result] = super
}
end
+
+ def remove(feature)
+ payload = {
+ :operation => :remove,
+ :adapter_name => name,
+ :feature_name => feature.name,
+ }
+
+ @instrumenter.instrument(InstrumentationName, payload) { |payload|
+ payload[:result] = super
+ }
+ end
+
+ def clear(feature)
+ payload = {
+ :operation => :clear,
+ :adapter_name => name,
+ :feature_name => feature.name,
+ }
+
+ @instrumenter.instrument(InstrumentationName, payload) { |payload|
+ payload[:result] = super
+ }
+ end
end
end
end
@@ -67,6 +67,23 @@ def add(feature)
result
end
+ # Public
+ def remove(feature)
+ result = super
+ if memoizing?
+ cache.delete(FeaturesKey)
+ cache.delete(feature)
+ end
+ result
+ end
+
+ # Public
+ def clear(feature)
+ result = super
+ cache.delete(feature) if memoizing?
+ result
+ end
+
# Internal
def cache
self.class.cache
@@ -52,9 +52,7 @@ def enable(feature, gate, thing)
def disable(feature, gate, thing)
case gate.data_type
when :boolean
- feature.gates.each do |gate|
- delete key(feature, gate)
- end
+ clear(feature)
when :integer
write key(feature, gate), thing.value.to_s
when :set
@@ -69,7 +67,21 @@ def disable(feature, gate, thing)
# Public: Adds a feature to the set of known features.
def add(feature)
features.add(feature.name.to_s)
+ true
+ end
+
+ # Public: Clears all the gate values for a feature.
+ def clear(feature)
+ feature.gates.each do |gate|
+ delete key(feature, gate)
+ end
+ end
+ # Public: Removes a feature from the set of known features and clears
+ # all the values for the feature.
+ def remove(feature)
+ features.delete(feature.name.to_s)
+ clear(feature)
true
end
@@ -161,13 +161,61 @@
result[:percentage_of_actors].should eq('10')
end
- it "can add and list known features" do
+ it "can add, remove and list known features" do
subject.features.should eq(Set.new)
subject.add(flipper[:stats]).should be_true
subject.features.should eq(Set['stats'])
subject.add(flipper[:search]).should be_true
subject.features.should eq(Set['stats', 'search'])
+
+ subject.remove(flipper[:stats]).should be_true
+ subject.features.should eq(Set['search'])
+
+ subject.remove(flipper[:search]).should be_true
+ subject.features.should eq(Set.new)
+ end
+
+ it "clears all the values for the feature on remove" do
+ actor_22 = actor_class.new('22')
+ subject.enable(feature, boolean_gate, flipper.boolean).should be_true
+ subject.enable(feature, group_gate, flipper.group(:admins)).should be_true
+ subject.enable(feature, actor_gate, flipper.actor(actor_22)).should be_true
+ subject.enable(feature, actors_gate, flipper.actors(25)).should be_true
+ subject.enable(feature, random_gate, flipper.random(45)).should be_true
+
+ subject.remove(feature).should be_true
+
+ subject.get(feature).should eq({
+ :boolean => nil,
+ :groups => Set.new,
+ :actors => Set.new,
+ :percentage_of_actors => nil,
+ :percentage_of_random => nil,
+ })
+ end
+
+ it "does not complain removing a feature that is already removed" do
+ subject.remove(flipper[:stats]).should be_true
+ end
+
+ it "can clear all the values for a feature" do
+ actor_22 = actor_class.new('22')
+ subject.enable(feature, boolean_gate, flipper.boolean).should be_true
+ subject.enable(feature, group_gate, flipper.group(:admins)).should be_true
+ subject.enable(feature, actor_gate, flipper.actor(actor_22)).should be_true
+ subject.enable(feature, actors_gate, flipper.actors(25)).should be_true
+ subject.enable(feature, random_gate, flipper.random(45)).should be_true
+
+ subject.clear(feature).should be_true
+
+ subject.get(feature).should eq({
+ :boolean => nil,
+ :groups => Set.new,
+ :actors => Set.new,
+ :percentage_of_actors => nil,
+ :percentage_of_random => nil,
+ })
end
end
@@ -77,6 +77,34 @@
end
end
+ describe "#remove" do
+ it "records instrumentation" do
+ result = subject.remove(feature)
+
+ event = instrumenter.events.last
+ event.should_not be_nil
+ event.name.should eq('adapter_operation.flipper')
+ event.payload[:operation].should eq(:remove)
+ event.payload[:adapter_name].should eq(:memory)
+ event.payload[:feature_name].should eq(:stats)
+ event.payload[:result].should be(result)
+ end
+ end
+
+ describe "#clear" do
+ it "records instrumentation" do
+ result = subject.clear(feature)
+
+ event = instrumenter.events.last
+ event.should_not be_nil
+ event.name.should eq('adapter_operation.flipper')
+ event.payload[:operation].should eq(:clear)
+ event.payload[:adapter_name].should eq(:memory)
+ event.payload[:feature_name].should eq(:stats)
+ event.payload[:result].should be(result)
+ end
+ end
+
describe "#features" do
it "records instrumentation" do
result = subject.features
@@ -164,7 +164,7 @@
subject.memoize = true
end
- it "unmemoizes features" do
+ it "unmemoizes the known features" do
cache[features_key] = {:some => 'thing'}
subject.add(flipper[:stats])
cache.should be_empty
@@ -181,4 +181,61 @@
end
end
end
+
+ describe "#remove" do
+ context "with memoization enabled" do
+ before do
+ subject.memoize = true
+ end
+
+ it "unmemoizes the known features" do
+ cache[features_key] = {:some => 'thing'}
+ subject.remove(flipper[:stats])
+ cache.should be_empty
+ end
+
+ it "unmemoizes the feature" do
+ feature = flipper[:stats]
+ cache[feature] = {:some => 'thing'}
+ subject.remove(feature)
+ cache[feature].should be_nil
+ end
+ end
+
+ context "with memoization disabled" do
+ before do
+ subject.memoize = false
+ end
+
+ it "returns result" do
+ subject.remove(flipper[:stats]).should eq(adapter.remove(flipper[:stats]))
+ end
+ end
+ end
+
+ describe "#clear" do
+ context "with memoization enabled" do
+ before do
+ subject.memoize = true
+ end
+
+ it "unmemoizes feature" do
+ feature = flipper[:stats]
+ cache[feature] = {:some => 'thing'}
+ subject.clear(feature)
+ cache[feature].should be_nil
+ end
+ end
+
+ context "with memoization disabled" do
+ before do
+ subject.memoize = false
+ end
+
+ it "returns result" do
+ feature = flipper[:stats]
+ subject.clear(feature).should eq(adapter.clear(feature))
+ end
+ end
+ end
end

0 comments on commit 58fbf84

Please sign in to comment.