-
-
Notifications
You must be signed in to change notification settings - Fork 413
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from jnunemaker/context
Feature Check Context
- Loading branch information
Showing
15 changed files
with
307 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require File.expand_path('../example_setup', __FILE__) | ||
|
||
require 'flipper' | ||
require 'flipper/adapters/memory' | ||
|
||
adapter = Flipper::Adapters::Memory.new | ||
flipper = Flipper.new(adapter) | ||
stats = flipper[:stats] | ||
|
||
# Register group | ||
Flipper.register(:enabled_team_member) do |actor, context| | ||
combos = context.actors_value.map { |flipper_id| flipper_id.split(":", 2) } | ||
team_names = combos.select { |class_name, id| class_name == "Team" }.map { |class_name, id| id } | ||
teams = team_names.map { |name| Team.find(name) } | ||
teams.any? { |team| team.member?(actor) } | ||
end | ||
|
||
# Some class that represents actor that will be trying to do something | ||
class User | ||
attr_reader :id | ||
|
||
def initialize(id) | ||
@id = id | ||
end | ||
|
||
def flipper_id | ||
"User:#{@id}" | ||
end | ||
end | ||
|
||
class Team | ||
attr_reader :name | ||
|
||
def self.all | ||
@all ||= {} | ||
end | ||
|
||
def self.find(name) | ||
all.fetch(name.to_s) | ||
end | ||
|
||
def initialize(name, members) | ||
@name = name.to_s | ||
@members = members | ||
self.class.all[@name] = self | ||
end | ||
|
||
def id | ||
@name | ||
end | ||
|
||
def member?(actor) | ||
@members.map(&:id).include?(actor.id) | ||
end | ||
|
||
def flipper_id | ||
"Team:#{@name}" | ||
end | ||
end | ||
|
||
jnunemaker = User.new("jnunemaker") | ||
jbarnette = User.new("jbarnette") | ||
aroben = User.new("aroben") | ||
|
||
core_app = Team.new(:core_app, [jbarnette, jnunemaker]) | ||
feature_flags = Team.new(:feature_flags, [aroben, jnunemaker]) | ||
|
||
stats.enable_actor jbarnette | ||
|
||
actors = [jbarnette, jnunemaker, aroben] | ||
|
||
actors.each do |actor| | ||
if stats.enabled?(actor) | ||
puts "stats are enabled for #{actor.id}" | ||
else | ||
puts "stats are NOT enabled for #{actor.id}" | ||
end | ||
end | ||
|
||
puts "enabling team_actor group" | ||
stats.enable_actor core_app | ||
stats.enable_group :enabled_team_member | ||
|
||
actors.each do |actor| | ||
if stats.enabled?(actor) | ||
puts "stats are enabled for #{actor.id}" | ||
else | ||
puts "stats are NOT enabled for #{actor.id}" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Flipper | ||
class FeatureCheckContext | ||
# Public: The name of the feature. | ||
attr_reader :feature_name | ||
|
||
# Public: The GateValues instance that keeps track of the values for the | ||
# gates for the feature. | ||
attr_reader :values | ||
|
||
# Public: The thing we want to know if a feature is enabled for. | ||
attr_reader :thing | ||
|
||
def initialize(options = {}) | ||
@feature_name = options.fetch(:feature_name) | ||
@values = options.fetch(:values) | ||
@thing = options.fetch(:thing) | ||
end | ||
|
||
# Public: Convenience method for groups value like Feature has. | ||
def groups_value | ||
values.groups | ||
end | ||
|
||
# Public: Convenience method for actors value value like Feature has. | ||
def actors_value | ||
values.actors | ||
end | ||
|
||
# Public: Convenience method for boolean value value like Feature has. | ||
def boolean_value | ||
values.boolean | ||
end | ||
|
||
# Public: Convenience method for percentage of actors value like Feature has. | ||
def percentage_of_actors_value | ||
values.percentage_of_actors | ||
end | ||
|
||
# Public: Convenience method for percentage of time value like Feature has. | ||
def percentage_of_time_value | ||
values.percentage_of_time | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
require 'helper' | ||
|
||
RSpec.describe Flipper::FeatureCheckContext do | ||
let(:feature_name) { :new_profiles } | ||
let(:values) { Flipper::GateValues.new({}) } | ||
let(:thing) { Struct.new(:flipper_id).new("5") } | ||
let(:options) { | ||
{ | ||
feature_name: feature_name, | ||
values: values, | ||
thing: thing, | ||
} | ||
} | ||
|
||
it "initializes just fine" do | ||
instance = described_class.new(options) | ||
expect(instance.feature_name).to eq(feature_name) | ||
expect(instance.values).to eq(values) | ||
expect(instance.thing).to eq(thing) | ||
end | ||
|
||
it "requires feature_name" do | ||
options.delete(:feature_name) | ||
expect { | ||
described_class.new(options) | ||
}.to raise_error(KeyError) | ||
end | ||
|
||
it "requires values" do | ||
options.delete(:values) | ||
expect { | ||
described_class.new(options) | ||
}.to raise_error(KeyError) | ||
end | ||
|
||
it "requires thing" do | ||
options.delete(:thing) | ||
expect { | ||
described_class.new(options) | ||
}.to raise_error(KeyError) | ||
end | ||
|
||
it "knows actors_value" do | ||
instance = described_class.new(options.merge(values: Flipper::GateValues.new({actors: Set["User:1"]}))) | ||
expect(instance.actors_value).to eq(Set["User:1"]) | ||
end | ||
|
||
it "knows groups_value" do | ||
instance = described_class.new(options.merge(values: Flipper::GateValues.new({groups: Set["admins"]}))) | ||
expect(instance.groups_value).to eq(Set["admins"]) | ||
end | ||
|
||
it "knows boolean_value" do | ||
instance = described_class.new(options.merge(values: Flipper::GateValues.new({boolean: true}))) | ||
expect(instance.boolean_value).to eq(true) | ||
end | ||
|
||
it "knows percentage_of_actors_value" do | ||
instance = described_class.new(options.merge(values: Flipper::GateValues.new({percentage_of_actors: 14}))) | ||
expect(instance.percentage_of_actors_value).to eq(14) | ||
end | ||
|
||
it "knows percentage_of_time_value" do | ||
instance = described_class.new(options.merge(values: Flipper::GateValues.new({percentage_of_time: 41}))) | ||
expect(instance.percentage_of_time_value).to eq(41) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.