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 Attribute Summary collapse

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



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

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

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



6
7
8
# File 'lib/featurevisor/child_instance.rb', line 6

def context
  @context
end

#emitterObject (readonly)

Returns the value of attribute emitter.



6
7
8
# File 'lib/featurevisor/child_instance.rb', line 6

def emitter
  @emitter
end

#parentObject (readonly)

Returns the value of attribute parent.



6
7
8
# File 'lib/featurevisor/child_instance.rb', line 6

def parent
  @parent
end

#stickyObject (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

#closeObject

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

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



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

#get_context(context = nil) ⇒ Hash

Get context

Parameters:

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

    Additional context to merge

Returns:

  • (Hash)

    Merged 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

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



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



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



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



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



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



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



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



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



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



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



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

Parameters:

  • context (Hash)

    Context to set

  • replace (Boolean) (defaults to: false)

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

Parameters:

  • sticky (Hash)

    Sticky features

  • replace (Boolean) (defaults to: false)

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