diff --git a/lib/flipper.rb b/lib/flipper.rb index bda5d731d..654a7d65a 100644 --- a/lib/flipper.rb +++ b/lib/flipper.rb @@ -64,7 +64,7 @@ def instance=(flipper) :enable_percentage_of_time, :disable_percentage_of_time, :time, :percentage_of_time, :features, :feature, :[], :preload, :preload_all, - :adapter, :add, :remove, :import, + :adapter, :add, :exist?, :remove, :import, :memoize=, :memoizing? # Public: Use this to register a group by name. diff --git a/lib/flipper/dsl.rb b/lib/flipper/dsl.rb index 871956b62..bc3114b08 100644 --- a/lib/flipper/dsl.rb +++ b/lib/flipper/dsl.rb @@ -151,6 +151,15 @@ def add(name) feature(name).add end + # Public: Has a feature been added in the adapter. + # + # name - The String or Symbol name of the feature. + # + # Returns true if added else false. + def exist?(name) + feature(name).exist? + end + # Public: Remove a feature. # # name - The String or Symbol name of the feature. diff --git a/lib/flipper/feature.rb b/lib/flipper/feature.rb index 268e96ccc..b11ca149d 100644 --- a/lib/flipper/feature.rb +++ b/lib/flipper/feature.rb @@ -76,6 +76,13 @@ def add instrument(:add) { adapter.add(self) } end + # Public: Does this feature exist in the adapter. + # + # Returns true if exists in adapter else false. + def exist? + instrument(:exist?) { adapter.features.include?(key) } + end + # Public: Removes this feature. # # Returns the result of Adapter#remove. diff --git a/spec/flipper/dsl_spec.rb b/spec/flipper/dsl_spec.rb index 02914c59f..4d98d7e70 100644 --- a/spec/flipper/dsl_spec.rb +++ b/spec/flipper/dsl_spec.rb @@ -312,6 +312,17 @@ end end + describe '#exist?' do + it 'returns true if the feature is added in adapter' do + subject.add(:stats) + expect(subject.exist?(:stats)).to be(true) + end + + it 'returns false if the feature is NOT added in adapter' do + expect(subject.exist?(:stats)).to be(false) + end + end + describe '#remove' do it 'removes the feature' do subject.adapter.add(subject[:stats]) diff --git a/spec/flipper/feature_spec.rb b/spec/flipper/feature_spec.rb index 06af6e623..77551af56 100644 --- a/spec/flipper/feature_spec.rb +++ b/spec/flipper/feature_spec.rb @@ -96,6 +96,17 @@ end end + describe '#exist?' do + it 'returns true if feature is added in adapter' do + subject.add + expect(subject.exist?).to be(true) + end + + it 'returns false if feature is NOT added in adapter' do + expect(subject.exist?).to be(false) + end + end + describe '#remove' do it 'removes feature from adapter' do adapter.add(subject) @@ -231,6 +242,17 @@ expect(event.payload[:result]).not_to be_nil end + it 'is recorded for exist?' do + subject.exist? + + event = instrumenter.events.last + expect(event).not_to be_nil + expect(event.name).to eq('feature_operation.flipper') + expect(event.payload[:feature_name]).to eq(:search) + expect(event.payload[:operation]).to eq(:exist?) + expect(event.payload[:result]).not_to be_nil + end + it 'is recorded for remove' do subject.remove diff --git a/spec/flipper_spec.rb b/spec/flipper_spec.rb index 616104bd1..b02814f60 100644 --- a/spec/flipper_spec.rb +++ b/spec/flipper_spec.rb @@ -187,6 +187,10 @@ expect(described_class.add(:search)).to eq(described_class.instance.add(:search)) end + it 'delegates exist? to instance' do + expect(described_class.exist?(:search)).to eq(described_class.instance.exist?(:search)) + end + it 'delegates remove to instance' do expect(described_class.remove(:search)).to eq(described_class.instance.remove(:search)) end