Class: Featurevisor::ChildInstance
- Inherits:
-
Object
- Object
- Featurevisor::ChildInstance
- Defined in:
- lib/featurevisor/child_instance.rb
Overview
Child instance with isolated context and sticky state.
Instance Method Summary collapse
- #close ⇒ Object
- #evaluate_flag(feature_key, context = {}, options = {}) ⇒ Object
- #evaluate_variable(feature_or_variable_key, variable_key_or_context = nil, context_or_options = {}, options = {}) ⇒ Object
- #evaluate_variation(feature_key, context = {}, options = {}) ⇒ Object
- #get_context(context = nil) ⇒ Object
- #get_feature_evaluations(context = {}, feature_keys = [], options = {}) ⇒ Object
- #get_variable(feature_or_variable_key, variable_key_or_context = nil, context_or_options = {}, options = {}) ⇒ Object
- #get_variable_evaluations(context = {}, variable_keys = [], options = {}) ⇒ Object
- #get_variation(feature_key, context = {}, options = {}) ⇒ Object
-
#initialize(options) ⇒ ChildInstance
constructor
A new instance of ChildInstance.
- #is_enabled(feature_key, context = {}, options = {}) ⇒ Object
- #on(event_name, callback = nil, &block) ⇒ Object
- #set_context(context, replace = false) ⇒ Object
- #set_sticky_features(sticky, replace = false) ⇒ Object
- #set_sticky_variables(sticky, replace = false) ⇒ Object
Constructor Details
#initialize(options) ⇒ ChildInstance
Returns a new instance of ChildInstance.
6 7 8 9 10 11 12 13 |
# File 'lib/featurevisor/child_instance.rb', line 6 def initialize() @parent = [:parent] @context = [:context] || {} @sticky_features = [:sticky_features] || {} @sticky_variables = [:sticky_variables] || {} @emitter = Featurevisor::Emitter.new @parent_unsubscribers = [] end |
Instance Method Details
#close ⇒ Object
35 36 37 38 39 |
# File 'lib/featurevisor/child_instance.rb', line 35 def close @parent_unsubscribers.dup.each(&:call) @parent_unsubscribers.clear @emitter.clear_all end |
#evaluate_flag(feature_key, context = {}, options = {}) ⇒ Object
70 71 72 |
# File 'lib/featurevisor/child_instance.rb', line 70 def evaluate_flag(feature_key, context = {}, = {}) @parent.evaluate_flag(feature_key, child_context(context), ()) end |
#evaluate_variable(feature_or_variable_key, variable_key_or_context = nil, context_or_options = {}, options = {}) ⇒ Object
86 87 88 |
# File 'lib/featurevisor/child_instance.rb', line 86 def evaluate_variable(feature_or_variable_key, variable_key_or_context = nil, = {}, = {}) delegate_variable(:evaluate_variable, feature_or_variable_key, variable_key_or_context, , ) end |
#evaluate_variation(feature_key, context = {}, options = {}) ⇒ Object
78 79 80 |
# File 'lib/featurevisor/child_instance.rb', line 78 def evaluate_variation(feature_key, context = {}, = {}) @parent.evaluate_variation(feature_key, child_context(context), ()) end |
#get_context(context = nil) ⇒ Object
46 47 48 |
# File 'lib/featurevisor/child_instance.rb', line 46 def get_context(context = nil) @parent.get_context({ **@context, **(context || {}) }) end |
#get_feature_evaluations(context = {}, feature_keys = [], options = {}) ⇒ Object
96 97 98 |
# File 'lib/featurevisor/child_instance.rb', line 96 def get_feature_evaluations(context = {}, feature_keys = [], = {}) @parent.get_feature_evaluations(child_context(context), feature_keys, ()) end |
#get_variable(feature_or_variable_key, variable_key_or_context = nil, context_or_options = {}, options = {}) ⇒ Object
82 83 84 |
# File 'lib/featurevisor/child_instance.rb', line 82 def get_variable(feature_or_variable_key, variable_key_or_context = nil, = {}, = {}) delegate_variable(:get_variable, feature_or_variable_key, variable_key_or_context, , ) end |
#get_variable_evaluations(context = {}, variable_keys = [], options = {}) ⇒ Object
100 101 102 |
# File 'lib/featurevisor/child_instance.rb', line 100 def get_variable_evaluations(context = {}, variable_keys = [], = {}) @parent.get_variable_evaluations(child_context(context), variable_keys, ()) end |
#get_variation(feature_key, context = {}, options = {}) ⇒ Object
74 75 76 |
# File 'lib/featurevisor/child_instance.rb', line 74 def get_variation(feature_key, context = {}, = {}) @parent.get_variation(feature_key, child_context(context), ()) end |
#is_enabled(feature_key, context = {}, options = {}) ⇒ Object
66 67 68 |
# File 'lib/featurevisor/child_instance.rb', line 66 def is_enabled(feature_key, context = {}, = {}) @parent.is_enabled(feature_key, child_context(context), ()) end |
#on(event_name, callback = nil, &block) ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/featurevisor/child_instance.rb', line 15 def on(event_name, callback = nil, &block) callback = block if block_given? if %w[context_set sticky_features_set sticky_variables_set].include?(event_name) return @emitter.on(event_name, callback) end 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 |
#set_context(context, replace = false) ⇒ Object
41 42 43 44 |
# File 'lib/featurevisor/child_instance.rb', line 41 def set_context(context, replace = false) @context = replace ? context : { **@context, **context } @emitter.trigger("context_set", context: @context, replaced: replace) end |
#set_sticky_features(sticky, replace = false) ⇒ Object
50 51 52 53 54 55 |
# File 'lib/featurevisor/child_instance.rb', line 50 def set_sticky_features(sticky, replace = false) previous = @sticky_features @sticky_features = replace ? sticky : { **@sticky_features, **sticky } params = Featurevisor::Events.get_params_for_sticky_features_set_event(previous, @sticky_features, replace) @emitter.trigger("sticky_features_set", params) end |
#set_sticky_variables(sticky, replace = false) ⇒ Object
57 58 59 60 61 62 63 64 |
# File 'lib/featurevisor/child_instance.rb', line 57 def set_sticky_variables(sticky, replace = false) previous = @sticky_variables @sticky_variables = replace ? sticky : { **@sticky_variables, **sticky } @emitter.trigger( "sticky_variables_set", Featurevisor::Events.get_params_for_sticky_variables_set_event(previous, @sticky_variables, replace) ) end |