Skip to content

Commit

Permalink
Merge pull request #371 from jnunemaker/exist
Browse files Browse the repository at this point in the history
Add exist to feature, dsl and flipper default instance delegation
  • Loading branch information
jnunemaker authored Jul 31, 2018
2 parents d67db88 + ec98f72 commit 7623cd1
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/flipper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
9 changes: 9 additions & 0 deletions lib/flipper/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 7 additions & 0 deletions lib/flipper/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions spec/flipper/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
22 changes: 22 additions & 0 deletions spec/flipper/feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions spec/flipper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 7623cd1

Please sign in to comment.