Class: Featurevisor::Instance
- Inherits:
-
Object
- Object
- Featurevisor::Instance
- Defined in:
- lib/featurevisor/instance.rb
Overview
Instance class for managing feature flag evaluations
Constant Summary collapse
- EMPTY_DATAFILE =
Empty datafile template
{ schemaVersion: "2", revision: "unknown", segments: {}, features: {} }.freeze
Instance Method Summary collapse
-
#add_module(mod) ⇒ Proc?
Add a module.
-
#close ⇒ Object
Close the instance.
-
#evaluate_flag(feature_key, context = {}, options = {}) ⇒ Hash
Evaluate a flag.
-
#evaluate_variable(feature_key, variable_key, context = {}, options = {}) ⇒ Hash
Evaluate a variable.
-
#evaluate_variation(feature_key, context = {}, options = {}) ⇒ Hash
Evaluate a variation.
-
#get_all_evaluations(context = {}, feature_keys = [], options = {}) ⇒ Hash
Get all evaluations.
-
#get_context(context = nil) ⇒ Hash
Get context.
-
#get_feature(feature_key) ⇒ Hash?
Get a feature by key.
- #get_feature_keys ⇒ Object
-
#get_revision ⇒ String
Get the revision.
- #get_schema_version ⇒ Object
- #get_segment(segment_key) ⇒ Object
-
#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_keys(feature_key) ⇒ Object
-
#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.
- #has_variations?(feature_key) ⇒ Boolean
-
#initialize(options = {}) ⇒ Instance
constructor
Initialize a new Featurevisor instance.
-
#is_enabled(feature_key, context = {}, options = {}) ⇒ Boolean
Check if a feature is enabled.
-
#on(event_name, callback) ⇒ Proc
Subscribe to an event.
- #remove_module(name) ⇒ Object
-
#set_context(context, replace = false) ⇒ Object
Set context.
-
#set_datafile(datafile, replace = false) ⇒ Object
Set the datafile.
-
#set_log_level(level) ⇒ Object
Set the log level.
-
#set_sticky(sticky, replace = false) ⇒ Object
Set sticky features.
-
#spawn(context = {}, options = {}) ⇒ ChildInstance
Spawn a child instance.
Constructor Details
#initialize(options = {}) ⇒ Instance
Initialize a new Featurevisor instance
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 |
# File 'lib/featurevisor/instance.rb', line 25 def initialize( = {}) # from options @context = [:context] || {} @diagnostics = DiagnosticReporter.new( level: [:log_level] || "info", handler: method(:handle_evaluation_diagnostic) ) @on_diagnostic = [:on_diagnostic] || [:onDiagnostic] @emitter = Featurevisor::Emitter.new @sticky = [:sticky] || {} @closed = false @module_diagnostic_subscriptions = [] # datafile @datafile = InstanceEvaluationDataProvider.new( datafile: EMPTY_DATAFILE, diagnostics: @diagnostics ) @modules_manager = Featurevisor::Modules::ModulesManager.new( modules: [:modules] || [], report_diagnostic: method(:report_diagnostic), module_api_factory: method(:create_module_api), clear_module_diagnostic_subscriptions: method(:clear_module_diagnostic_subscriptions) ) if [:datafile] set_datafile([:datafile], true) end report_diagnostic( level: "info", code: "sdk_initialized", message: "SDK initialized" ) end |
Instance Method Details
#add_module(mod) ⇒ Proc?
Add a module
202 203 204 |
# File 'lib/featurevisor/instance.rb', line 202 def add_module(mod) @modules_manager.add(mod) end |
#close ⇒ Object
Close the instance
221 222 223 224 225 226 227 228 |
# File 'lib/featurevisor/instance.rb', line 221 def close return if @closed @closed = true @modules_manager.close_all @module_diagnostic_subscriptions = [] @emitter.clear_all end |
#evaluate_flag(feature_key, context = {}, options = {}) ⇒ Hash
Evaluate a flag
287 288 289 290 291 292 293 294 |
# File 'lib/featurevisor/instance.rb', line 287 def evaluate_flag(feature_key, context = {}, = {}) Featurevisor::Evaluate.evaluate_with_modules( get_evaluation_dependencies(context, ).merge( type: "flag", feature_key: feature_key ) ) end |
#evaluate_variable(feature_key, variable_key, context = {}, options = {}) ⇒ Hash
Evaluate a variable
353 354 355 356 357 358 359 360 361 |
# File 'lib/featurevisor/instance.rb', line 353 def evaluate_variable(feature_key, variable_key, context = {}, = {}) Featurevisor::Evaluate.evaluate_with_modules( get_evaluation_dependencies(context, ).merge( type: "variable", feature_key: feature_key, variable_key: variable_key ) ) end |
#evaluate_variation(feature_key, context = {}, options = {}) ⇒ Hash
Evaluate a variation
316 317 318 319 320 321 322 323 |
# File 'lib/featurevisor/instance.rb', line 316 def evaluate_variation(feature_key, context = {}, = {}) Featurevisor::Evaluate.evaluate_with_modules( get_evaluation_dependencies(context, ).merge( type: "variation", feature_key: feature_key ) ) end |
#get_all_evaluations(context = {}, feature_keys = [], options = {}) ⇒ Hash
Get all evaluations
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 |
# File 'lib/featurevisor/instance.rb', line 472 def get_all_evaluations(context = {}, feature_keys = [], = {}) result = {} keys = feature_keys.size > 0 ? feature_keys : @datafile.get_feature_keys keys.each do |feature_key| # Convert symbol keys to strings for evaluation functions feature_key_str = feature_key.to_s # isEnabled evaluated_feature = { enabled: is_enabled(feature_key_str, context, ) } # variation if @datafile.has_variations?(feature_key_str) variation = get_variation(feature_key_str, context, ) evaluated_feature[:variation] = variation unless variation.nil? end # variables variable_keys = @datafile.get_variable_keys(feature_key_str) if variable_keys.size > 0 evaluated_feature[:variables] = {} variable_keys.each do |variable_key| evaluated_feature[:variables][variable_key] = get_variable( feature_key_str, variable_key, context, ) end end result[feature_key] = evaluated_feature end result end |
#get_context(context = nil) ⇒ Hash
Get context
259 260 261 262 263 264 265 266 267 268 |
# File 'lib/featurevisor/instance.rb', line 259 def get_context(context = nil) if context { **@context, **context } else @context end end |
#get_feature(feature_key) ⇒ Hash?
Get a feature by key
195 196 197 |
# File 'lib/featurevisor/instance.rb', line 195 def get_feature(feature_key) @datafile.get_feature(feature_key) end |
#get_feature_keys ⇒ Object
180 181 182 |
# File 'lib/featurevisor/instance.rb', line 180 def get_feature_keys @datafile.get_feature_keys end |
#get_revision ⇒ String
Get the revision
168 169 170 |
# File 'lib/featurevisor/instance.rb', line 168 def get_revision @datafile.get_revision end |
#get_schema_version ⇒ Object
172 173 174 |
# File 'lib/featurevisor/instance.rb', line 172 def get_schema_version @datafile.get_schema_version end |
#get_segment(segment_key) ⇒ Object
176 177 178 |
# File 'lib/featurevisor/instance.rb', line 176 def get_segment(segment_key) @datafile.get_segment(segment_key) end |
#get_variable(feature_key, variable_key, context = {}, options = {}) ⇒ Object?
Get variable value
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 |
# File 'lib/featurevisor/instance.rb', line 369 def get_variable(feature_key, variable_key, context = {}, = {}) begin evaluation = evaluate_variable(feature_key, variable_key, context, ) if evaluation.key?(:variable_value) if evaluation[:variable_schema] && evaluation[:variable_schema][:type] == "json" && evaluation[:variable_value].is_a?(String) JSON.parse(evaluation[:variable_value], symbolize_names: true) else evaluation[:variable_value] end else nil end rescue => e report_diagnostic(level: "error", code: "evaluation_error", message: "getVariable failed", originalError: e, details: { featureKey: feature_key, variableKey: variable_key }) nil end end |
#get_variable_array(feature_key, variable_key, context = {}, options = {}) ⇒ Array?
Get variable as array
440 441 442 443 |
# File 'lib/featurevisor/instance.rb', line 440 def get_variable_array(feature_key, variable_key, context = {}, = {}) variable_value = get_variable(feature_key, variable_key, context, ) get_value_by_type(variable_value, "array") end |
#get_variable_boolean(feature_key, variable_key, context = {}, options = {}) ⇒ Boolean?
Get variable as boolean
396 397 398 399 |
# File 'lib/featurevisor/instance.rb', line 396 def get_variable_boolean(feature_key, variable_key, context = {}, = {}) variable_value = get_variable(feature_key, variable_key, context, ) get_value_by_type(variable_value, "boolean") end |
#get_variable_double(feature_key, variable_key, context = {}, options = {}) ⇒ Float?
Get variable as double
429 430 431 432 |
# File 'lib/featurevisor/instance.rb', line 429 def get_variable_double(feature_key, variable_key, context = {}, = {}) variable_value = get_variable(feature_key, variable_key, context, ) get_value_by_type(variable_value, "double") end |
#get_variable_integer(feature_key, variable_key, context = {}, options = {}) ⇒ Integer?
Get variable as integer
418 419 420 421 |
# File 'lib/featurevisor/instance.rb', line 418 def get_variable_integer(feature_key, variable_key, context = {}, = {}) variable_value = get_variable(feature_key, variable_key, context, ) get_value_by_type(variable_value, "integer") end |
#get_variable_json(feature_key, variable_key, context = {}, options = {}) ⇒ Object?
Get variable as JSON
462 463 464 465 |
# File 'lib/featurevisor/instance.rb', line 462 def get_variable_json(feature_key, variable_key, context = {}, = {}) variable_value = get_variable(feature_key, variable_key, context, ) get_value_by_type(variable_value, "json") end |
#get_variable_keys(feature_key) ⇒ Object
184 185 186 |
# File 'lib/featurevisor/instance.rb', line 184 def get_variable_keys(feature_key) @datafile.get_variable_keys(feature_key) end |
#get_variable_object(feature_key, variable_key, context = {}, options = {}) ⇒ Hash?
Get variable as object
451 452 453 454 |
# File 'lib/featurevisor/instance.rb', line 451 def get_variable_object(feature_key, variable_key, context = {}, = {}) variable_value = get_variable(feature_key, variable_key, context, ) get_value_by_type(variable_value, "object") end |
#get_variable_string(feature_key, variable_key, context = {}, options = {}) ⇒ String?
Get variable as string
407 408 409 410 |
# File 'lib/featurevisor/instance.rb', line 407 def get_variable_string(feature_key, variable_key, context = {}, = {}) variable_value = get_variable(feature_key, variable_key, context, ) get_value_by_type(variable_value, "string") end |
#get_variation(feature_key, context = {}, options = {}) ⇒ String?
Get variation value
330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/featurevisor/instance.rb', line 330 def get_variation(feature_key, context = {}, = {}) begin evaluation = evaluate_variation(feature_key, context, ) if evaluation.key?(:variation_value) evaluation[:variation_value] elsif evaluation[:variation] evaluation[:variation][:value] else nil end rescue => e report_diagnostic(level: "error", code: "evaluation_error", message: "getVariation failed", originalError: e, details: { featureKey: feature_key }) nil end end |
#has_variations?(feature_key) ⇒ Boolean
188 189 190 |
# File 'lib/featurevisor/instance.rb', line 188 def has_variations?(feature_key) @datafile.has_variations?(feature_key) end |
#is_enabled(feature_key, context = {}, options = {}) ⇒ Boolean
Check if a feature is enabled
301 302 303 304 305 306 307 308 309 |
# File 'lib/featurevisor/instance.rb', line 301 def is_enabled(feature_key, context = {}, = {}) begin evaluation = evaluate_flag(feature_key, context, ) evaluation[:enabled] == true rescue => e report_diagnostic(level: "error", code: "evaluation_error", message: "isEnabled failed", originalError: e, details: { featureKey: feature_key }) false end end |
#on(event_name, callback) ⇒ Proc
Subscribe to an event
214 215 216 217 218 |
# File 'lib/featurevisor/instance.rb', line 214 def on(event_name, callback) return -> {} if @closed @emitter.on(event_name, callback) end |
#remove_module(name) ⇒ Object
206 207 208 |
# File 'lib/featurevisor/instance.rb', line 206 def remove_module(name) @modules_manager.remove(name) end |
#set_context(context, replace = false) ⇒ Object
Set context
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'lib/featurevisor/instance.rb', line 233 def set_context(context, replace = false) if replace @context = context else @context = { **@context, **context } end @emitter.trigger("context_set", { context: @context, replaced: replace }) report_diagnostic( level: "debug", code: "context_set", message: replace ? "Context replaced" : "Context updated", details: { context: @context, replaced: replace } ) end |
#set_datafile(datafile, replace = false) ⇒ Object
Set the datafile
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/featurevisor/instance.rb', line 98 def set_datafile(datafile, replace = false) return if @closed begin parsed_datafile = if datafile.is_a?(String) JSON.parse(datafile, symbolize_names: true) elsif datafile.is_a?(Hash) JSON.parse(JSON.generate(datafile), symbolize_names: true) else datafile end unless parsed_datafile.is_a?(Hash) && parsed_datafile[:schemaVersion].is_a?(String) && parsed_datafile[:revision].is_a?(String) && parsed_datafile[:segments].is_a?(Hash) && parsed_datafile[:features].is_a?(Hash) raise ArgumentError, "Invalid datafile" end next_datafile = replace ? parsed_datafile : merge_datafiles(@datafile.get_datafile, parsed_datafile) new_datafile = InstanceEvaluationDataProvider.new( datafile: next_datafile, diagnostics: @diagnostics ) details = Featurevisor::Events.get_params_for_datafile_set_event(@datafile, new_datafile, replace) @datafile = new_datafile report_diagnostic( level: "info", code: "datafile_set", message: "Datafile set", details: details ) @emitter.trigger("datafile_set", details) rescue => e report_diagnostic( level: "error", code: "invalid_datafile", message: "Could not parse datafile", original_error: e ) end end |
#set_log_level(level) ⇒ Object
Set the log level
64 65 66 |
# File 'lib/featurevisor/instance.rb', line 64 def set_log_level(level) @diagnostics.set_level(level) end |
#set_sticky(sticky, replace = false) ⇒ Object
Set sticky features
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/featurevisor/instance.rb', line 143 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) report_diagnostic( level: "info", code: "sticky_set", message: "Sticky features set", details: params ) @emitter.trigger("sticky_set", params) end |
#spawn(context = {}, options = {}) ⇒ ChildInstance
Spawn a child instance
274 275 276 277 278 279 280 |
# File 'lib/featurevisor/instance.rb', line 274 def spawn(context = {}, = {}) Featurevisor::ChildInstance.new( parent: self, context: get_context(context), sticky: [:sticky] ) end |