Class: Featurevisor::ChildInstance
- Inherits:
-
Object
- Object
- Featurevisor::ChildInstance
- Defined in:
- lib/featurevisor/child_instance.rb
Overview
Child instance class for managing child contexts and sticky features
Instance Method Summary collapse
-
#close ⇒ Object
Close the child instance.
-
#evaluate_flag(feature_key, context = {}, options = {}) ⇒ Object
Evaluate a feature flag and return its full evaluation details.
-
#evaluate_variable(feature_key, variable_key, context = {}, options = {}) ⇒ Object
Evaluate a variable and return its full evaluation details.
-
#evaluate_variation(feature_key, context = {}, options = {}) ⇒ Object
Evaluate a variation and return its full evaluation details.
-
#get_all_evaluations(context = {}, feature_keys = [], options = {}) ⇒ Hash
Get all evaluations.
-
#get_context(context = nil) ⇒ Hash
Get context.
-
#get_variable(feature_key, variable_key, context = {}, options = {}) ⇒ Object?
Get variable value.
-
#get_variable_array(feature_key, variable_key, context = {}, options = {}) ⇒ Array?
Get variable as array.
-
#get_variable_boolean(feature_key, variable_key, context = {}, options = {}) ⇒ Boolean?
Get variable as boolean.
-
#get_variable_double(feature_key, variable_key, context = {}, options = {}) ⇒ Float?
Get variable as double.
-
#get_variable_integer(feature_key, variable_key, context = {}, options = {}) ⇒ Integer?
Get variable as integer.
-
#get_variable_json(feature_key, variable_key, context = {}, options = {}) ⇒ Object?
Get variable as JSON.
-
#get_variable_object(feature_key, variable_key, context = {}, options = {}) ⇒ Hash?
Get variable as object.
-
#get_variable_string(feature_key, variable_key, context = {}, options = {}) ⇒ String?
Get variable as string.
-
#get_variation(feature_key, context = {}, options = {}) ⇒ String?
Get variation value.
-
#initialize(options) ⇒ ChildInstance
constructor
Initialize a new child instance.
-
#is_enabled(feature_key, context = {}, options = {}) ⇒ Boolean
Check if a feature is enabled.
-
#on(event_name, callback = nil, &block) ⇒ Proc
Subscribe to an event.
-
#set_context(context, replace = false) ⇒ Object
Set context.
-
#set_sticky(sticky, replace = false) ⇒ Object
Set sticky features.
Constructor Details
#initialize(options) ⇒ ChildInstance
Initialize a new child instance
11 12 13 14 15 16 17 |
# File 'lib/featurevisor/child_instance.rb', line 11 def initialize() @parent = [:parent] @context = [:context] || {} @sticky = [:sticky] || {} @emitter = Featurevisor::Emitter.new @parent_unsubscribers = [] end |
Instance Method Details
#close ⇒ Object
Close the child instance
45 46 47 48 49 |
# File 'lib/featurevisor/child_instance.rb', line 45 def close @parent_unsubscribers.dup.each(&:call) @parent_unsubscribers.clear @emitter.clear_all end |
#evaluate_flag(feature_key, context = {}, options = {}) ⇒ Object
Evaluate a feature flag and return its full evaluation details.
116 117 118 119 120 121 122 |
# File 'lib/featurevisor/child_instance.rb', line 116 def evaluate_flag(feature_key, context = {}, = {}) @parent.evaluate_flag( feature_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#evaluate_variable(feature_key, variable_key, context = {}, options = {}) ⇒ Object
Evaluate a variable and return its full evaluation details.
174 175 176 177 178 179 180 181 |
# File 'lib/featurevisor/child_instance.rb', line 174 def evaluate_variable(feature_key, variable_key, context = {}, = {}) @parent.evaluate_variable( feature_key, variable_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#evaluate_variation(feature_key, context = {}, options = {}) ⇒ Object
Evaluate a variation and return its full evaluation details.
144 145 146 147 148 149 150 |
# File 'lib/featurevisor/child_instance.rb', line 144 def evaluate_variation(feature_key, context = {}, = {}) @parent.evaluate_variation( feature_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#get_all_evaluations(context = {}, feature_keys = [], options = {}) ⇒ Hash
Get all evaluations
335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/featurevisor/child_instance.rb', line 335 def get_all_evaluations(context = {}, feature_keys = [], = {}) @parent.get_all_evaluations( { **@context, **context }, feature_keys, { **, __featurevisor_child_sticky: @sticky } ) end |
#get_context(context = nil) ⇒ Hash
Get context
70 71 72 73 74 75 |
# File 'lib/featurevisor/child_instance.rb', line 70 def get_context(context = nil) @parent.get_context({ **@context, **(context || {}) }) end |
#get_variable(feature_key, variable_key, context = {}, options = {}) ⇒ Object?
Get variable value
158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/featurevisor/child_instance.rb', line 158 def get_variable(feature_key, variable_key, context = {}, = {}) @parent.get_variable( feature_key, variable_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#get_variable_array(feature_key, variable_key, context = {}, options = {}) ⇒ Array?
Get variable as array
273 274 275 276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/featurevisor/child_instance.rb', line 273 def get_variable_array(feature_key, variable_key, context = {}, = {}) @parent.get_variable_array( feature_key, variable_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#get_variable_boolean(feature_key, variable_key, context = {}, options = {}) ⇒ Boolean?
Get variable as boolean
189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
# File 'lib/featurevisor/child_instance.rb', line 189 def get_variable_boolean(feature_key, variable_key, context = {}, = {}) @parent.get_variable_boolean( feature_key, variable_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#get_variable_double(feature_key, variable_key, context = {}, options = {}) ⇒ Float?
Get variable as double
252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
# File 'lib/featurevisor/child_instance.rb', line 252 def get_variable_double(feature_key, variable_key, context = {}, = {}) @parent.get_variable_double( feature_key, variable_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#get_variable_integer(feature_key, variable_key, context = {}, options = {}) ⇒ Integer?
Get variable as integer
231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/featurevisor/child_instance.rb', line 231 def get_variable_integer(feature_key, variable_key, context = {}, = {}) @parent.get_variable_integer( feature_key, variable_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#get_variable_json(feature_key, variable_key, context = {}, options = {}) ⇒ Object?
Get variable as JSON
315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
# File 'lib/featurevisor/child_instance.rb', line 315 def get_variable_json(feature_key, variable_key, context = {}, = {}) @parent.get_variable_json( feature_key, variable_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#get_variable_object(feature_key, variable_key, context = {}, options = {}) ⇒ Hash?
Get variable as object
294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/featurevisor/child_instance.rb', line 294 def get_variable_object(feature_key, variable_key, context = {}, = {}) @parent.get_variable_object( feature_key, variable_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#get_variable_string(feature_key, variable_key, context = {}, options = {}) ⇒ String?
Get variable as string
210 211 212 213 214 215 216 217 218 219 220 221 222 223 |
# File 'lib/featurevisor/child_instance.rb', line 210 def get_variable_string(feature_key, variable_key, context = {}, = {}) @parent.get_variable_string( feature_key, variable_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#get_variation(feature_key, context = {}, options = {}) ⇒ String?
Get variation value
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/featurevisor/child_instance.rb', line 129 def get_variation(feature_key, context = {}, = {}) @parent.get_variation( feature_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#is_enabled(feature_key, context = {}, options = {}) ⇒ Boolean
Check if a feature is enabled
101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/featurevisor/child_instance.rb', line 101 def is_enabled(feature_key, context = {}, = {}) @parent.is_enabled( feature_key, { **@context, **context }, { **, __featurevisor_child_sticky: @sticky } ) end |
#on(event_name, callback = nil, &block) ⇒ Proc
Subscribe to an event
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/featurevisor/child_instance.rb', line 23 def on(event_name, callback = nil, &block) callback = block if block_given? if event_name == "context_set" || event_name == "sticky_set" @emitter.on(event_name, callback) else parent_unsubscribe = @parent.on(event_name, callback) active = true unsubscribe = nil unsubscribe = proc do next unless active active = false parent_unsubscribe.call @parent_unsubscribers.delete(unsubscribe) end @parent_unsubscribers << unsubscribe unsubscribe end end |
#set_context(context, replace = false) ⇒ Object
Set context
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/featurevisor/child_instance.rb', line 54 def set_context(context, replace = false) if replace @context = context else @context = { **@context, **context } end @emitter.trigger("context_set", { context: @context, replaced: replace }) end |
#set_sticky(sticky, replace = false) ⇒ Object
Set sticky features
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/featurevisor/child_instance.rb', line 80 def set_sticky(sticky, replace = false) previous_sticky_features = @sticky || {} if replace @sticky = sticky else @sticky = { **@sticky, **sticky } end params = Featurevisor::Events.get_params_for_sticky_set_event(previous_sticky_features, @sticky, replace) @emitter.trigger("sticky_set", params) end |