@@ -68,6 +68,32 @@ def disable(thing = false)
6868 end
6969 end
7070
71+ def block ( thing )
72+ instrument ( :block ) do |payload |
73+ adapter . add self
74+
75+ blocking_gate = gate_for ( thing , blocking : true )
76+ wrapped_thing = blocking_gate . wrap ( thing )
77+ payload [ :gate_name ] = blocking_gate . name
78+ payload [ :thing ] = wrapped_thing
79+
80+ adapter . enable self , blocking_gate , wrapped_thing
81+ end
82+ end
83+
84+ def unblock ( thing )
85+ instrument ( :unblock ) do |payload |
86+ adapter . add self
87+
88+ blocking_gate = gate_for ( thing , blocking : true )
89+ wrapped_thing = blocking_gate . wrap ( thing )
90+ payload [ :gate_name ] = blocking_gate . name
91+ payload [ :thing ] = wrapped_thing
92+
93+ adapter . disable self , blocking_gate , wrapped_thing
94+ end
95+ end
96+
7197 # Public: Adds this feature.
7298 #
7399 # Returns the result of Adapter#add.
@@ -118,7 +144,10 @@ def enabled?(*actors)
118144 actors : actors
119145 )
120146
121- if open_gate = gates . detect { |gate | gate . open? ( context ) }
147+ if blocking_gate = gates . detect { |gate | gate . blocks? ( context ) }
148+ payload [ :gate_name ] = blocking_gate . name
149+ false
150+ elsif open_gate = gates . detect { |gate | gate . open? ( context ) }
122151 payload [ :gate_name ] = open_gate . name
123152 true
124153 else
@@ -250,6 +279,22 @@ def disable_percentage_of_actors
250279 disable Types ::PercentageOfActors . new ( 0 )
251280 end
252281
282+ def block_actor ( actor )
283+ block Types ::Actor . wrap ( actor )
284+ end
285+
286+ def block_group ( group )
287+ block Types ::Group . wrap ( group )
288+ end
289+
290+ def unblock_actor ( actor )
291+ unblock Types ::Actor . wrap ( actor )
292+ end
293+
294+ def unblock_group ( group )
295+ unblock Types ::Group . wrap ( group )
296+ end
297+
253298 # Public: Returns state for feature (:on, :off, or :conditional).
254299 def state
255300 values = gate_values
@@ -413,6 +458,8 @@ def gates_hash
413458 percentage_of_actors : Gates ::PercentageOfActors . new ,
414459 percentage_of_time : Gates ::PercentageOfTime . new ,
415460 group : Gates ::Group . new ,
461+ blocking_actor : Gates ::Actor . new ( { blocking : true } ) ,
462+ blocking_group : Gates ::Group . new ( { blocking : true } ) ,
416463 } . freeze
417464 end
418465
@@ -429,8 +476,8 @@ def gate(name)
429476 #
430477 # Returns a Flipper::Gate.
431478 # Raises Flipper::GateNotFound if no gate found for actor
432- def gate_for ( actor )
433- gates . detect { |gate | gate . protects? ( actor ) } || raise ( GateNotFound , actor )
479+ def gate_for ( actor , blocking : false )
480+ gates . detect { |gate | gate . protects? ( actor ) && gate . blocking == blocking } || raise ( GateNotFound , actor )
434481 end
435482
436483 private
0 commit comments