forked from flippercloud/flipper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_with_members.rb
94 lines (73 loc) · 1.73 KB
/
group_with_members.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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(:team_actor) do |actor|
actor.is_a?(TeamActor) && actor.allowed?
end
# Some class that represents actor that will be trying to do something
class User
attr_reader :id
def initialize(id)
@id = id
end
# Must respond to flipper_id
alias_method :flipper_id, :id
end
class Team
attr_reader :name
def initialize(name, members)
@name = name
@members = members
end
def id
@name
end
def member?(actor)
@members.include?(actor)
end
end
class TeamActor
def initialize(team, actor)
@team = team
@actor = actor
end
def allowed?
@team.member?(@actor)
end
def flipper_id
"TeamActor:#{@team.id}:#{@actor.id}"
end
end
jnunemaker = User.new(1)
jbarnette = User.new(2)
aroben = User.new(3)
core_app = Team.new(:core_app, [jbarnette, jnunemaker])
feature_flags = Team.new(:feature_flags, [aroben, jnunemaker])
core_nunes = TeamActor.new(core_app, jnunemaker)
core_roben = TeamActor.new(core_app, aroben)
if stats.enabled?(core_nunes)
puts "stats are enabled for jnunemaker"
else
puts "stats are NOT enabled for jnunemaker"
end
if stats.enabled?(core_roben)
puts "stats are enabled for aroben"
else
puts "stats are NOT enabled for aroben"
end
puts "enabling team_actor group"
stats.enable_group :team_actor
if stats.enabled?(core_nunes)
puts "stats are enabled for jnunemaker"
else
puts "stats are NOT enabled for jnunemaker"
end
if stats.enabled?(core_roben)
puts "stats are enabled for aroben"
else
puts "stats are NOT enabled for aroben"
end