Class: LaunchDarkly::Integrations::TestDataV2::FlagBuilderV2

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb

Overview

A builder for feature flag configurations to be used with LaunchDarkly::Integrations::TestDataV2.

See Also:

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ FlagBuilderV2

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of FlagBuilderV2.



28
29
30
31
32
33
34
35
36
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 28

def initialize(key)
  @_key = key
  @_on = true
  @_variations = []
  @_off_variation = nil
  @_fallthrough_variation = nil
  @_targets = {}
  @_rules = []
end

Instance Attribute Details

#_keyObject (readonly)

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



25
26
27
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 25

def _key
  @_key
end

Instance Method Details

#add_rule(flag_rule_builder) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



390
391
392
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 390

def add_rule(flag_rule_builder)
  @_rules << flag_rule_builder
end

#boolean_flagFlagBuilderV2

A shortcut for setting the flag to use the standard boolean configuration.

This is the default for all new flags created with LaunchDarkly::Integrations::TestDataV2#flag.

The flag will have two variations, true and false (in that order); it will return false whenever targeting is off, and true when targeting is on if no other settings specify otherwise.

Returns:



122
123
124
125
126
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 122

def boolean_flag
  return self if boolean_flag?

  variations(true, false).fallthrough_variation(TRUE_VARIATION_INDEX).off_variation(FALSE_VARIATION_INDEX)
end

#build(version) ⇒ Hash

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Note that build is private by convention, because we don’t want developers to consider it part of the public API, but it is still called from TestDataV2.

Creates a dictionary representation of the flag

Parameters:

  • version (Integer)

    the version number of the flag

Returns:

  • (Hash)

    the dictionary representation of the flag



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 352

def build(version)
  base_flag_object = {
    key: @_key,
    version: version,
    on: @_on,
    variations: @_variations,
    prerequisites: [],
    salt: '',
  }

  base_flag_object[:offVariation] = @_off_variation unless @_off_variation.nil?
  base_flag_object[:fallthrough] = { variation: @_fallthrough_variation } unless @_fallthrough_variation.nil?

  targets = []
  context_targets = []
  @_targets.each do |target_context_kind, target_variations|
    target_variations.each do |var_index, target_keys|
      if target_context_kind == LaunchDarkly::LDContext::KIND_DEFAULT
        targets << { variation: var_index, values: target_keys.to_a.sort } # sorting just for test determinacy
        context_targets << { contextKind: target_context_kind, variation: var_index, values: [] }
      else
        context_targets << { contextKind: target_context_kind, variation: var_index, values: target_keys.to_a.sort } # sorting just for test determinacy
      end
    end
  end
  base_flag_object[:targets] = targets unless targets.empty?
  base_flag_object[:contextTargets] = context_targets unless context_targets.empty?

  rules = []
  @_rules.each_with_index do |rule, idx|
    rules << rule.build(idx.to_s)
  end
  base_flag_object[:rules] = rules unless rules.empty?

  base_flag_object
end

#clear_rulesFlagBuilderV2

Removes any existing rules from the flag. This undoes the effect of methods like #if_match.

Returns:



327
328
329
330
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 327

def clear_rules
  @_rules = []
  self
end

#clear_targetsFlagBuilderV2

Removes any existing targets from the flag. This undoes the effect of methods like #variation_for_user.

Returns:



338
339
340
341
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 338

def clear_targets
  @_targets = {}
  self
end

#fallthrough_variation(variation) ⇒ FlagBuilderV2

Specifies the fallthrough variation. The fallthrough is the value that is returned if targeting is on and the context was not matched by a more specific target or rule.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.

Parameters:

  • variation (Boolean, Integer)

    true or false or the desired fallthrough variation index: 0 for the first, 1 for the second, etc.

Returns:



82
83
84
85
86
87
88
89
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 82

def fallthrough_variation(variation)
  if LaunchDarkly::Impl::Util.bool?(variation)
    boolean_flag.fallthrough_variation(TestDataV2.variation_for_boolean(variation))
  else
    @_fallthrough_variation = variation
    self
  end
end

#if_match(attribute, *values) ⇒ FlagRuleBuilderV2

Starts defining a flag rule, using the “is one of” operator.

This is a shortcut for calling #if_match_context with LaunchDarkly::LDContext::KIND_DEFAULT as the context kind.

Examples:

create a rule that returns true if the name is “Patsy” or “Edina”

td.flag("flag")
    .if_match('name', 'Patsy', 'Edina')
    .then_return(true)

Parameters:

  • attribute (String)

    the user attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:



258
259
260
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 258

def if_match(attribute, *values)
  if_match_context(LaunchDarkly::LDContext::KIND_DEFAULT, attribute, *values)
end

#if_match_context(context_kind, attribute, *values) ⇒ FlagRuleBuilderV2

Starts defining a flag rule, using the “is one of” operator. This matching expression only applies to contexts of a specific kind.

Examples:

create a rule that returns true if the name attribute for the

"company" context is "Ella" or "Monsoon":
td.flag("flag")
    .if_match_context('company', 'name', 'Ella', 'Monsoon')
    .then_return(True)

Parameters:

  • context_kind (String)

    the context kind

  • attribute (String)

    the context attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:



277
278
279
280
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 277

def if_match_context(context_kind, attribute, *values)
  flag_rule_builder = FlagRuleBuilderV2.new(self)
  flag_rule_builder.and_match_context(context_kind, attribute, *values)
end

#if_not_match(attribute, *values) ⇒ FlagRuleBuilderV2

Starts defining a flag rule, using the “is not one of” operator.

This is a shortcut for calling #if_not_match_context with LaunchDarkly::LDContext::KIND_DEFAULT as the context kind.

Examples:

create a rule that returns true if the name is neither “Saffron” nor “Bubble”

td.flag("flag")
    .if_not_match('name', 'Saffron', 'Bubble')
    .then_return(true)

Parameters:

  • attribute (String)

    the user attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:



297
298
299
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 297

def if_not_match(attribute, *values)
  if_not_match_context(LaunchDarkly::LDContext::KIND_DEFAULT, attribute, *values)
end

#if_not_match_context(context_kind, attribute, *values) ⇒ FlagRuleBuilderV2

Starts defining a flag rule, using the “is not one of” operator. This matching expression only applies to contexts of a specific kind.

Examples:

create a rule that returns true if the name attribute for the

"company" context is neither "Pendant" nor "Sterling Cooper":
td.flag("flag")
    .if_not_match_context('company', 'name', 'Pendant', 'Sterling Cooper')
    .then_return(true)

Parameters:

  • context_kind (String)

    the context kind

  • attribute (String)

    the context attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:



316
317
318
319
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 316

def if_not_match_context(context_kind, attribute, *values)
  flag_rule_builder = FlagRuleBuilderV2.new(self)
  flag_rule_builder.and_not_match_context(context_kind, attribute, *values)
end

#initialize_copy(other) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Creates a deep copy of the flag builder when the object is duplicated or cloned. Subsequent updates to the original FlagBuilderV2 object will not update the copy and vice versa.

This method is automatically invoked by Ruby’s dup and clone methods. Immutable instance variables (strings, numbers, booleans, nil) are automatically copied by the super call. Only mutable collections need explicit deep copying.



47
48
49
50
51
52
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 47

def initialize_copy(other)
  super(other)
  @_variations = @_variations.clone
  @_targets = deep_copy_targets
  @_rules = deep_copy_rules
end

#off_variation(variation) ⇒ FlagBuilderV2

Specifies the off variation. This is the variation that is returned whenever targeting is off.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.

Parameters:

  • variation (Boolean, Integer)

    true or false or the desired off variation index: 0 for the first, 1 for the second, etc.

Returns:



102
103
104
105
106
107
108
109
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 102

def off_variation(variation)
  if LaunchDarkly::Impl::Util.bool?(variation)
    boolean_flag.off_variation(TestDataV2.variation_for_boolean(variation))
  else
    @_off_variation = variation
    self
  end
end

#on(on) ⇒ FlagBuilderV2

Sets targeting to be on or off for this flag.

The effect of this depends on the rest of the flag configuration, just as it does on the real LaunchDarkly dashboard. In the default configuration that you get from calling LaunchDarkly::Integrations::TestDataV2#flag with a new flag key, the flag will return false whenever targeting is off, and true when targeting is on.

Parameters:

  • on (Boolean)

    true if targeting should be on

Returns:



65
66
67
68
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 65

def on(on)
  @_on = on
  self
end

#value_for_all(value) ⇒ FlagBuilderV2

Sets the flag to always return the specified variation value for all contexts.

The value may be of any valid JSON type. This method changes the flag to have only a single variation, which is this value, and to return the same variation regardless of whether targeting is on or off. Any existing targets or rules are removed.

Parameters:

  • value (Object)

    the desired value to be returned for all contexts

Returns:



181
182
183
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 181

def value_for_all(value)
  variations(value).variation_for_all(0)
end

#variation_for_all(variation) ⇒ FlagBuilderV2

Sets the flag to always return the specified variation for all contexts.

The variation is specified, targeting is switched on, and any existing targets or rules are removed. The fallthrough variation is set to the specified value. The off variation is left unchanged.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.

Parameters:

  • variation (Boolean, Integer)

    true or false or the desired variation index to return: 0 for the first, 1 for the second, etc.

Returns:



162
163
164
165
166
167
168
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 162

def variation_for_all(variation)
  if LaunchDarkly::Impl::Util.bool?(variation)
    return boolean_flag.variation_for_all(TestDataV2.variation_for_boolean(variation))
  end

  clear_rules.clear_targets.on(true).fallthrough_variation(variation)
end

#variation_for_key(context_kind, context_key, variation) ⇒ FlagBuilderV2

Sets the flag to return the specified variation for a specific context, identified by context kind and key, when targeting is on.

This has no effect when targeting is turned off for the flag.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.

Parameters:

  • context_kind (String)

    the context kind

  • context_key (String)

    the context key

  • variation (Boolean, Integer)

    true or false or the desired variation index to return: 0 for the first, 1 for the second, etc.

Returns:



221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 221

def variation_for_key(context_kind, context_key, variation)
  if LaunchDarkly::Impl::Util.bool?(variation)
    return boolean_flag.variation_for_key(context_kind, context_key, TestDataV2.variation_for_boolean(variation))
  end

  targets = @_targets[context_kind]
  if targets.nil?
    targets = {}
    @_targets[context_kind] = targets
  end

  @_variations.each_index do |idx|
    if idx == variation
      (targets[idx] ||= Set.new).add(context_key)
    elsif targets.key?(idx)
      targets[idx].delete(context_key)
    end
  end

  self
end

#variation_for_user(user_key, variation) ⇒ FlagBuilderV2

Sets the flag to return the specified variation for a specific user key when targeting is on.

This is a shortcut for calling #variation_for_key with LaunchDarkly::LDContext::KIND_DEFAULT as the context kind.

This has no effect when targeting is turned off for the flag.

If the flag was previously configured with other variations and the variation specified is a boolean, this also changes it to a boolean flag.

Parameters:

  • user_key (String)

    a user key

  • variation (Boolean, Integer)

    true or false or the desired variation index to return: 0 for the first, 1 for the second, etc.

Returns:



202
203
204
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 202

def variation_for_user(user_key, variation)
  variation_for_key(LaunchDarkly::LDContext::KIND_DEFAULT, user_key, variation)
end

#variations(*variations) ⇒ FlagBuilderV2

Changes the allowable variation values for the flag.

The value may be of any valid JSON type. For instance, a boolean flag normally has ‘true, false`; a string-valued flag might have `’red’, ‘green’‘; etc.

Examples:

A single variation

td.flag('new-flag').variations(true)

Multiple variations

td.flag('new-flag').variations('red', 'green', 'blue')

Parameters:

  • variations (Array<Object>)

    the desired variations

Returns:



144
145
146
147
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 144

def variations(*variations)
  @_variations = variations
  self
end