@@ -24,9 +24,11 @@ class OptimizelyUserContext
2424
2525 attr_reader :user_id
2626 attr_reader :forced_decisions
27- attr_reader :ForcedDecision
27+ attr_reader :OptimizelyDecisionContext
28+ attr_reader :OptimizelyForcedDecision
2829
29- ForcedDecision = Struct . new ( :flag_key , :rule_key )
30+ OptimizelyDecisionContext = Struct . new ( :flag_key , :rule_key )
31+ OptimizelyForcedDecision = Struct . new ( :variation_key )
3032 def initialize ( optimizely_client , user_id , user_attributes )
3133 @attr_mutex = Mutex . new
3234 @forced_decision_mutex = Mutex . new
@@ -94,59 +96,54 @@ def decide_all(options = nil)
9496
9597 # Sets the forced decision (variation key) for a given flag and an optional rule.
9698 #
97- # @param flag_key - A flag key.
98- # @param rule_key - An experiment or delivery rule key (optional).
99- # @param variation_key - A variation key.
99+ # @param context - An OptimizelyDecisionContext object containg flag key and rule key.
100+ # @param decision - An OptimizelyForcedDecision object containing variation key
100101 #
101102 # @return - true if the forced decision has been set successfully.
102103
103- def set_forced_decision ( flag_key , rule_key , variation_key )
104+ def set_forced_decision ( context , decision )
105+ flag_key = context [ :flag_key ]
104106 return false if @optimizely_client &.get_optimizely_config . nil?
105- return false if flag_key . empty? || flag_key . nil?
107+ return false if flag_key . nil?
106108
107- forced_decision_key = ForcedDecision . new ( flag_key , rule_key )
108- @forced_decision_mutex . synchronize { @forced_decisions [ forced_decision_key ] = variation_key }
109+ @forced_decision_mutex . synchronize { @forced_decisions [ context ] = decision }
109110
110111 true
111112 end
112113
113- def find_forced_decision ( flag_key , rule_key = nil )
114+ def find_forced_decision ( context )
114115 return nil if @forced_decisions . empty?
115116
116- variation_key = nil
117- forced_decision_key = ForcedDecision . new ( flag_key , rule_key )
118- @forced_decision_mutex . synchronize { variation_key = @forced_decisions [ forced_decision_key ] }
119- variation_key
117+ decision = nil
118+ @forced_decision_mutex . synchronize { decision = @forced_decisions [ context ] }
119+ decision
120120 end
121121
122122 # Returns the forced decision for a given flag and an optional rule.
123123 #
124- # @param flag_key - A flag key.
125- # @param rule_key - An experiment or delivery rule key (optional).
124+ # @param context - An OptimizelyDecisionContext object containg flag key and rule key.
126125 #
127126 # @return - A variation key or nil if forced decisions are not set for the parameters.
128127
129- def get_forced_decision ( flag_key , rule_key = nil )
128+ def get_forced_decision ( context )
130129 return nil if @optimizely_client &.get_optimizely_config . nil?
131130
132- find_forced_decision ( flag_key , rule_key )
131+ find_forced_decision ( context )
133132 end
134133
135134 # Removes the forced decision for a given flag and an optional rule.
136135 #
137- # @param flag_key - A flag key.
138- # @param rule_key - An experiment or delivery rule key (optional).
136+ # @param context - An OptimizelyDecisionContext object containg flag key and rule key.
139137 #
140138 # @return - true if the forced decision has been removed successfully.
141139
142- def remove_forced_decision ( flag_key , rule_key = nil )
140+ def remove_forced_decision ( context )
143141 return false if @optimizely_client &.get_optimizely_config . nil?
144142
145- forced_decision_key = ForcedDecision . new ( flag_key , rule_key )
146143 deleted = false
147144 @forced_decision_mutex . synchronize do
148- if @forced_decisions . key? ( forced_decision_key )
149- @forced_decisions . delete ( forced_decision_key )
145+ if @forced_decisions . key? ( context )
146+ @forced_decisions . delete ( context )
150147 deleted = true
151148 end
152149 end
@@ -164,8 +161,11 @@ def remove_all_forced_decision
164161 true
165162 end
166163
167- def find_validated_forced_decision ( flag_key , rule_key )
168- variation_key = find_forced_decision ( flag_key , rule_key )
164+ def find_validated_forced_decision ( context )
165+ decision = find_forced_decision ( context )
166+ flag_key = context [ :flag_key ]
167+ rule_key = context [ :rule_key ]
168+ variation_key = decision ? decision [ :variation_key ] : decision
169169 reasons = [ ]
170170 target = rule_key ? "flag (#{ flag_key } ), rule (#{ rule_key } )" : "flag (#{ flag_key } )"
171171 if variation_key
0 commit comments