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

Add #features_for_actor to return all enabled features for a given actor #783

Open
wants to merge 2 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/flipper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def instance=(flipper)
:enable_group, :disable_group,
:enable_percentage_of_actors, :disable_percentage_of_actors,
:enable_percentage_of_time, :disable_percentage_of_time,
:features, :feature, :[], :preload, :preload_all,
:features, :feature, :features_for_actor, :[], :preload, :preload_all,
:adapter, :add, :exist?, :remove, :import, :export,
:memoize=, :memoizing?, :read_only?,
:sync, :sync_secret # For Flipper::Cloud. Will error for OSS Flipper.
Expand Down
12 changes: 11 additions & 1 deletion lib/flipper/dsl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ def enable_actor(name, actor)
feature(name).enable_actor(actor)
end

# Public: All Enabled features for an actor.
#
# actor - a Flipper::Types::Actor instance or an object that responds
# to flipper_id.
#
# Returns Set of Flipper::Feature instances.
def features_for_actor(actor)
adapter.features.keep_if { |feature_name| enabled?(feature_name, actor) }.to_set { |name| feature(name) }
end

# Public: Enable a feature for a group.
#
# name - The String or Symbol name of the feature.
Expand Down Expand Up @@ -269,7 +279,7 @@ def expression(name)
#
# Returns Set of Flipper::Feature instances.
def features
adapter.features.map { |name| feature(name) }.to_set
adapter.features.to_set { |name| feature(name) }
end

# Public: Does this adapter support writes or not.
Expand Down
25 changes: 25 additions & 0 deletions spec/flipper/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,31 @@
end
end

describe '#features_for_actor' do
jnunemaker marked this conversation as resolved.
Show resolved Hide resolved
actor = Flipper::Actor.new(5)
context 'with no features' do
it 'defaults to empty set' do
expect(subject.features_for_actor(actor)).to eq(Set.new)
end
end

context 'with features enabled and disabled' do
before do
subject.enable_actor(:stats, actor)
subject.enable_actor(:cache, actor)
subject[:search].disable
end

it 'returns set of feature instances' do
expect(subject.features_for_actor(actor)).to be_instance_of(Set)
subject.features_for_actor(actor).each do |feature|
expect(feature).to be_instance_of(Flipper::Feature)
end
expect(subject.features_for_actor(actor).map(&:name).map(&:to_s).sort).to eq(%w(cache stats))
end
end
end

describe '#enable/disable' do
it 'enables and disables the feature' do
expect(subject[:stats].boolean_value).to eq(false)
Expand Down