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 Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#emitter ⇒ Object
readonly
Returns the value of attribute emitter.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#sticky ⇒ Object
readonly
Returns the value of attribute sticky.
Instance Method Summary collapse
-
#close ⇒ Object
Close the child instance.
-
#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
13 14 15 16 17 18 |
# File 'lib/featurevisor/child_instance.rb', line 13 def initialize() @parent = [:parent] @context = [:context] || {} @sticky = [:sticky] || {} @emitter = Featurevisor::Emitter.new end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
6 7 8 |
# File 'lib/featurevisor/child_instance.rb', line 6 def context @context end |
#emitter ⇒ Object (readonly)
Returns the value of attribute emitter.
6 7 8 |
# File 'lib/featurevisor/child_instance.rb', line 6 def emitter @emitter end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
6 7 8 |
# File 'lib/featurevisor/child_instance.rb', line 6 def parent @parent end |
#sticky ⇒ Object (readonly)
Returns the value of attribute sticky.
6 7 8 |
# File 'lib/featurevisor/child_instance.rb', line 6 def sticky @sticky end |
Instance Method Details
#close ⇒ Object
Close the child instance
35 36 37 |
# File 'lib/featurevisor/child_instance.rb', line 35 def close @emitter.clear_all end |
#get_all_evaluations(context = {}, feature_keys = [], options = {}) ⇒ Hash
Get all evaluations
295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/featurevisor/child_instance.rb', line 295 def get_all_evaluations(context = {}, feature_keys = [], = {}) @parent.get_all_evaluations( { **@context, **context }, feature_keys, { sticky: @sticky, ** } ) end |
#get_context(context = nil) ⇒ Hash
Get context
58 59 60 61 62 63 |
# File 'lib/featurevisor/child_instance.rb', line 58 def get_context(context = nil) @parent.get_context({ **@context, **(context || {}) }) end |
#get_variable(feature_key, variable_key, context = {}, options = {}) ⇒ Object?
Get variable value
128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/featurevisor/child_instance.rb', line 128 def get_variable(feature_key, variable_key, context = {}, = {}) @parent.get_variable( feature_key, variable_key, { **@context, **context }, { sticky: @sticky, ** } ) end |
#get_variable_array(feature_key, variable_key, context = {}, options = {}) ⇒ Array?
Get variable as array
233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/featurevisor/child_instance.rb', line 233 def get_variable_array(feature_key, variable_key, context = {}, = {}) @parent.get_variable_array( feature_key, variable_key, { **@context, **context }, { sticky: @sticky, ** } ) end |
#get_variable_boolean(feature_key, variable_key, context = {}, options = {}) ⇒ Boolean?
Get variable as boolean
149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
# File 'lib/featurevisor/child_instance.rb', line 149 def get_variable_boolean(feature_key, variable_key, context = {}, = {}) @parent.get_variable_boolean( feature_key, variable_key, { **@context, **context }, { sticky: @sticky, ** } ) end |
#get_variable_double(feature_key, variable_key, context = {}, options = {}) ⇒ Float?
Get variable as double
212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/featurevisor/child_instance.rb', line 212 def get_variable_double(feature_key, variable_key, context = {}, = {}) @parent.get_variable_double( feature_key, variable_key, { **@context, **context }, { sticky: @sticky, ** } ) end |
#get_variable_integer(feature_key, variable_key, context = {}, options = {}) ⇒ Integer?
Get variable as integer
191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/featurevisor/child_instance.rb', line 191 def get_variable_integer(feature_key, variable_key, context = {}, = {}) @parent.get_variable_integer( feature_key, variable_key, { **@context, **context }, { sticky: @sticky, ** } ) end |
#get_variable_json(feature_key, variable_key, context = {}, options = {}) ⇒ Object?
Get variable as JSON
275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/featurevisor/child_instance.rb', line 275 def get_variable_json(feature_key, variable_key, context = {}, = {}) @parent.get_variable_json( feature_key, variable_key, { **@context, **context }, { sticky: @sticky, ** } ) end |
#get_variable_object(feature_key, variable_key, context = {}, options = {}) ⇒ Hash?
Get variable as object
254 255 256 257 258 259 260 261 262 263 264 265 266 267 |
# File 'lib/featurevisor/child_instance.rb', line 254 def get_variable_object(feature_key, variable_key, context = {}, = {}) @parent.get_variable_object( feature_key, variable_key, { **@context, **context }, { sticky: @sticky, ** } ) end |
#get_variable_string(feature_key, variable_key, context = {}, options = {}) ⇒ String?
Get variable as string
170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
# File 'lib/featurevisor/child_instance.rb', line 170 def get_variable_string(feature_key, variable_key, context = {}, = {}) @parent.get_variable_string( feature_key, variable_key, { **@context, **context }, { sticky: @sticky, ** } ) end |
#get_variation(feature_key, context = {}, options = {}) ⇒ String?
Get variation value
108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/featurevisor/child_instance.rb', line 108 def get_variation(feature_key, context = {}, = {}) @parent.get_variation( feature_key, { **@context, **context }, { sticky: @sticky, ** } ) end |
#is_enabled(feature_key, context = {}, options = {}) ⇒ Boolean
Check if a feature is enabled
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/featurevisor/child_instance.rb', line 89 def is_enabled(feature_key, context = {}, = {}) @parent.is_enabled( feature_key, { **@context, **context }, { sticky: @sticky, ** } ) end |
#on(event_name, callback = nil, &block) ⇒ Proc
Subscribe to an event
24 25 26 27 28 29 30 31 32 |
# File 'lib/featurevisor/child_instance.rb', line 24 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.on(event_name, callback) end end |
#set_context(context, replace = false) ⇒ Object
Set context
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/featurevisor/child_instance.rb', line 42 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
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/featurevisor/child_instance.rb', line 68 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 |