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
# File 'lib/featurevisor/child_instance.rb', line 11

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

Instance Method Details

#closeObject

Close the child instance



33
34
35
# File 'lib/featurevisor/child_instance.rb', line 33

def close
  @emitter.clear_all
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



293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'lib/featurevisor/child_instance.rb', line 293

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



56
57
58
59
60
61
# File 'lib/featurevisor/child_instance.rb', line 56

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



126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/featurevisor/child_instance.rb', line 126

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



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_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



147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/featurevisor/child_instance.rb', line 147

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



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_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



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_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



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_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



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_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



168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/featurevisor/child_instance.rb', line 168

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



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/featurevisor/child_instance.rb', line 106

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



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/featurevisor/child_instance.rb', line 87

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



22
23
24
25
26
27
28
29
30
# File 'lib/featurevisor/child_instance.rb', line 22

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

Parameters:

  • context (Hash)

    Context to set

  • replace (Boolean) (defaults to: false)

    Whether to replace existing context



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/featurevisor/child_instance.rb', line 40

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



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/featurevisor/child_instance.rb', line 66

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