Class: Featurevisor::ChildInstance

Inherits:
Object
  • Object
show all
Defined in:
lib/featurevisor/child_instance.rb

Overview

Child instance class for managing child contexts and sticky features

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ ChildInstance

Initialize a new child instance

Parameters:

  • options (Hash)

    Child instance options

Options Hash (options):

  • :parent (Instance)

    Parent instance

  • :context (Hash)

    Child context

  • :sticky (Hash)

    Child sticky features



11
12
13
14
15
16
17
# File 'lib/featurevisor/child_instance.rb', line 11

def initialize(options)
  @parent = options[:parent]
  @context = options[:context] || {}
  @sticky = options[:sticky] || {}
  @emitter = Featurevisor::Emitter.new
  @parent_unsubscribers = []
end

Instance Method Details

#closeObject

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 = {}, options = {})
  @parent.evaluate_flag(
    feature_key,
    { **@context, **context },
    { **options, __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 = {}, options = {})
  @parent.evaluate_variable(
    feature_key,
    variable_key,
    { **@context, **context },
    { **options, __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 = {}, options = {})
  @parent.evaluate_variation(
    feature_key,
    { **@context, **context },
    { **options, __featurevisor_child_sticky: @sticky }
  )
end

#get_all_evaluations(context = {}, feature_keys = [], options = {}) ⇒ Hash

Get all evaluations

Parameters:

  • context (Hash) (defaults to: {})

    Context

  • feature_keys (Array<String>) (defaults to: [])

    Feature keys to evaluate

  • options (Hash) (defaults to: {})

    Override options

Returns:

  • (Hash)

    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 = [], options = {})
  @parent.get_all_evaluations(
    {
      **@context,
      **context
    },
    feature_keys,
    {
      **options,
      __featurevisor_child_sticky: @sticky
    }
  )
end

#get_context(context = nil) ⇒ Hash

Get context

Parameters:

  • context (Hash, nil) (defaults to: nil)

    Additional context to merge

Returns:

  • (Hash)

    Merged 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

Parameters:

  • feature_key (String)

    Feature key

  • variable_key (String)

    Variable key

  • context (Hash) (defaults to: {})

    Context

  • options (Hash) (defaults to: {})

    Override options

Returns:

  • (Object, nil)

    Variable value or nil



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 = {}, options = {})
  @parent.get_variable(
    feature_key,
    variable_key,
    {
      **@context,
      **context
    },
    {
      **options,
      __featurevisor_child_sticky: @sticky
    }
  )
end

#get_variable_array(feature_key, variable_key, context = {}, options = {}) ⇒ Array?

Get variable as array

Parameters:

  • feature_key (String)

    Feature key

  • variable_key (String)

    Variable key

  • context (Hash) (defaults to: {})

    Context

  • options (Hash) (defaults to: {})

    Override options

Returns:

  • (Array, nil)

    Array value or nil



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 = {}, options = {})
  @parent.get_variable_array(
    feature_key,
    variable_key,
    {
      **@context,
      **context
    },
    {
      **options,
      __featurevisor_child_sticky: @sticky
    }
  )
end

#get_variable_boolean(feature_key, variable_key, context = {}, options = {}) ⇒ Boolean?

Get variable as boolean

Parameters:

  • feature_key (String)

    Feature key

  • variable_key (String)

    Variable key

  • context (Hash) (defaults to: {})

    Context

  • options (Hash) (defaults to: {})

    Override options

Returns:

  • (Boolean, nil)

    Boolean value or nil



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 = {}, options = {})
  @parent.get_variable_boolean(
    feature_key,
    variable_key,
    {
      **@context,
      **context
    },
    {
      **options,
      __featurevisor_child_sticky: @sticky
    }
  )
end

#get_variable_double(feature_key, variable_key, context = {}, options = {}) ⇒ Float?

Get variable as double

Parameters:

  • feature_key (String)

    Feature key

  • variable_key (String)

    Variable key

  • context (Hash) (defaults to: {})

    Context

  • options (Hash) (defaults to: {})

    Override options

Returns:

  • (Float, nil)

    Float value or nil



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 = {}, options = {})
  @parent.get_variable_double(
    feature_key,
    variable_key,
    {
      **@context,
      **context
    },
    {
      **options,
      __featurevisor_child_sticky: @sticky
    }
  )
end

#get_variable_integer(feature_key, variable_key, context = {}, options = {}) ⇒ Integer?

Get variable as integer

Parameters:

  • feature_key (String)

    Feature key

  • variable_key (String)

    Variable key

  • context (Hash) (defaults to: {})

    Context

  • options (Hash) (defaults to: {})

    Override options

Returns:

  • (Integer, nil)

    Integer value or nil



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 = {}, options = {})
  @parent.get_variable_integer(
    feature_key,
    variable_key,
    {
      **@context,
      **context
    },
    {
      **options,
      __featurevisor_child_sticky: @sticky
    }
  )
end

#get_variable_json(feature_key, variable_key, context = {}, options = {}) ⇒ Object?

Get variable as JSON

Parameters:

  • feature_key (String)

    Feature key

  • variable_key (String)

    Variable key

  • context (Hash) (defaults to: {})

    Context

  • options (Hash) (defaults to: {})

    Override options

Returns:

  • (Object, nil)

    JSON value or nil



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 = {}, options = {})
  @parent.get_variable_json(
    feature_key,
    variable_key,
    {
      **@context,
      **context
    },
    {
      **options,
      __featurevisor_child_sticky: @sticky
    }
  )
end

#get_variable_object(feature_key, variable_key, context = {}, options = {}) ⇒ Hash?

Get variable as object

Parameters:

  • feature_key (String)

    Feature key

  • variable_key (String)

    Variable key

  • context (Hash) (defaults to: {})

    Context

  • options (Hash) (defaults to: {})

    Override options

Returns:

  • (Hash, nil)

    Object value or nil



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 = {}, options = {})
  @parent.get_variable_object(
    feature_key,
    variable_key,
    {
      **@context,
      **context
    },
    {
      **options,
      __featurevisor_child_sticky: @sticky
    }
  )
end

#get_variable_string(feature_key, variable_key, context = {}, options = {}) ⇒ String?

Get variable as string

Parameters:

  • feature_key (String)

    Feature key

  • variable_key (String)

    Variable key

  • context (Hash) (defaults to: {})

    Context

  • options (Hash) (defaults to: {})

    Override options

Returns:

  • (String, nil)

    String value or nil



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 = {}, options = {})
  @parent.get_variable_string(
    feature_key,
    variable_key,
    {
      **@context,
      **context
    },
    {
      **options,
      __featurevisor_child_sticky: @sticky
    }
  )
end

#get_variation(feature_key, context = {}, options = {}) ⇒ String?

Get variation value

Parameters:

  • feature_key (String)

    Feature key

  • context (Hash) (defaults to: {})

    Context

  • options (Hash) (defaults to: {})

    Override options

Returns:

  • (String, nil)

    Variation value or nil



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 = {}, options = {})
  @parent.get_variation(
    feature_key,
    {
      **@context,
      **context
    },
    {
      **options,
      __featurevisor_child_sticky: @sticky
    }
  )
end

#is_enabled(feature_key, context = {}, options = {}) ⇒ Boolean

Check if a feature is enabled

Parameters:

  • feature_key (String)

    Feature key

  • context (Hash) (defaults to: {})

    Context

  • options (Hash) (defaults to: {})

    Override options

Returns:

  • (Boolean)

    True if 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 = {}, options = {})
  @parent.is_enabled(
    feature_key,
    {
      **@context,
      **context
    },
    {
      **options,
      __featurevisor_child_sticky: @sticky
    }
  )
end

#on(event_name, callback = nil, &block) ⇒ Proc

Subscribe to an event

Parameters:

  • event_name (String)

    Event name

  • callback (Proc) (defaults to: nil)

    Callback function

Returns:

  • (Proc)

    Unsubscribe function



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

Parameters:

  • context (Hash)

    Context to set

  • replace (Boolean) (defaults to: false)

    Whether to replace existing 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

Parameters:

  • sticky (Hash)

    Sticky features

  • replace (Boolean) (defaults to: false)

    Whether to replace existing 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