Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
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
24 changes: 24 additions & 0 deletions spec/flipper/dsl_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,30 @@
end
end
end
describe '#features_for_actor' do
Comment thread
jnunemaker marked this conversation as 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
Expand Down