Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use reject(&:nil?) in enabled? check #887

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/flipper/feature.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,9 @@ def clear
#
# Returns true if enabled, false if not.
def enabled?(*actors)
actors = actors.flatten.compact.map { |actor| Types::Actor.wrap(actor) }
# Allows null object pattern. See PR for more.
# https://github.com/flippercloud/flipper/pull/887
actors = actors.flatten.reject(&:nil?).map { |actor| Types::Actor.wrap(actor) }
jnunemaker marked this conversation as resolved.
Show resolved Hide resolved
actors = nil if actors.empty?

# thing is left for backwards compatibility
Expand Down
20 changes: 20 additions & 0 deletions spec/flipper/feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,26 @@
expect(subject.enabled?(actors)).to be(false)
end
end

context "for an object that implements .nil? == true" do
let(:actor) { Flipper::Actor.new("User;1") }

before do
def actor.nil?
true
end
end

it 'returns true if feature is enabled' do
subject.enable
expect(subject.enabled?(actor)).to be(true)
end

it 'returns false if feature is disabled' do
subject.disable
expect(subject.enabled?(actor)).to be(false)
end
end
end

describe '#to_s' do
Expand Down
Loading