Class: LaunchDarkly::Integrations::TestData::FlagBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/integrations/test_data/flag_builder.rb

Overview

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

See Also:

Since:

  • 6.3.0

Defined Under Namespace

Classes: FlagRuleBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key) ⇒ FlagBuilder

Returns a new instance of FlagBuilder.

Since:

  • 6.3.0



16
17
18
19
20
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 16

def initialize(key)
  @key = key
  @on = true
  @variations = []
end

Instance Attribute Details

#keyObject (readonly)

Since:

  • 6.3.0



13
14
15
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 13

def key
  @key
end

Instance Method Details

#add_rule(rule) ⇒ Object

Since:

  • 6.3.0



335
336
337
338
339
340
341
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 335

def add_rule(rule)
  if @rules.nil?
    @rules = Array.new
  end
  @rules.push(rule)
  self
end

#boolean_flagFlagBuilder

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

This is the default for all new flags created with LaunchDarkly::Integrations::TestData#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:

Since:

  • 6.3.0



353
354
355
356
357
358
359
360
361
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 353

def boolean_flag
  if boolean_flag?
    self
  else
    variations(true, false)
      .fallthrough_variation(TRUE_VARIATION_INDEX)
      .off_variation(FALSE_VARIATION_INDEX)
  end
end

#build(version) ⇒ Object

Since:

  • 6.3.0



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 364

def build(version)
  res = { key: @key,
          version: version,
          on: @on,
          variations: @variations,
        }

  unless @off_variation.nil?
    res[:offVariation] = @off_variation
  end

  unless @fallthrough_variation.nil?
    res[:fallthrough] = { variation: @fallthrough_variation }
  end

  unless @targets.nil?
    targets = []
    context_targets = []

    @targets.each do |kind, targets_for_kind|
      targets_for_kind.each_with_index do |values, variation|
        next if values.nil?
        if kind == LaunchDarkly::LDContext::KIND_DEFAULT
          targets << { variation: variation, values: values }
          context_targets << { contextKind: LaunchDarkly::LDContext::KIND_DEFAULT, variation: variation, values: [] }
        else
          context_targets << { contextKind: kind, variation: variation, values: values }
        end
      end
    end

    res[:targets] = targets
    res[:contextTargets] = context_targets
  end

  unless @rules.nil?
    res[:rules] = @rules.each_with_index.map { | rule, i | rule.build(i) }
  end

  res
end

#clear_rulesFlagBuilder

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

Returns:

Since:

  • 6.3.0



329
330
331
332
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 329

def clear_rules
  @rules = nil
  self
end

#clear_targetsFlagBuilder Also known as: clear_user_targets

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

Returns:

Since:

  • 6.3.0



313
314
315
316
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 313

def clear_targets
  @targets = nil
  self
end

#fallthrough_variation(variation) ⇒ FlagBuilder

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:

Since:

  • 6.3.0



58
59
60
61
62
63
64
65
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 58

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

#if_match(attribute, *values) ⇒ FlagRuleBuilder

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”

testData.flag("flag")
    .if_match(:name, 'Patsy', 'Edina')
    .then_return(true);

Parameters:

  • attribute (Symbol)

    the user attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:

See Also:

Since:

  • 6.3.0



259
260
261
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 259

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

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

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

Examples:

create a rule that returns ‘true` if the name is “Patsy” or “Edina” and the context kind is “user”

testData.flag("flag")
    .if_match_context("user", :name, 'Patsy', 'Edina')
    .then_return(true);

Parameters:

  • context_kind (String)

    a context kind

  • attribute (Symbol)

    the context attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:

See Also:

Since:

  • 6.3.0



236
237
238
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 236

def if_match_context(context_kind, attribute, *values)
  FlagRuleBuilder.new(self).and_match_context(context_kind, attribute, *values)
end

#if_not_match(attribute, *values) ⇒ FlagRuleBuilder

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”

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

Parameters:

  • attribute (Symbol)

    the user attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:

See Also:

Since:

  • 6.3.0



303
304
305
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 303

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) ⇒ FlagRuleBuilder

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

Examples:

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

testData.flag("flag")
    .if_not_match_context("user", :name, 'Saffron', 'Bubble')
    .then_return(true)

Parameters:

  • context_kind (String)

    a context kind

  • attribute (Symbol)

    the context attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:

See Also:

Since:

  • 6.3.0



280
281
282
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 280

def if_not_match_context(context_kind, attribute, *values)
  FlagRuleBuilder.new(self).and_not_match_context(context_kind, attribute, *values)
end

#initialize_copy(other) ⇒ Object

Since:

  • 6.3.0



23
24
25
26
27
28
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 23

def initialize_copy(other)
  super(other)
  @variations = @variations.clone
  @rules = @rules.nil? ? nil : deep_copy_array(@rules)
  @targets = @targets.nil? ? nil : deep_copy_hash(@targets)
end

#off_variation(variation) ⇒ FlagBuilder

Specifies the off variation for a flag. 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:

Since:

  • 6.3.0



78
79
80
81
82
83
84
85
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 78

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

#on(on) ⇒ FlagBuilder

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::TestData#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:

Since:

  • 6.3.0



41
42
43
44
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 41

def on(on)
  @on = on
  self
end

#value_for_all(value) ⇒ FlagBuilder Also known as: value_for_all_users

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

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:

Since:

  • 6.3.0



147
148
149
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 147

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

#variation_for_all(variation) ⇒ FlagBuilder Also known as: variation_for_all_users

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:

Since:

  • 6.3.0



123
124
125
126
127
128
129
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 123

def variation_for_all(variation)
  if LaunchDarkly::Impl::Util.bool? variation
    boolean_flag.variation_for_all(variation_for_boolean(variation))
  else
    on(true).clear_rules.clear_targets.fallthrough_variation(variation)
  end
end

#variation_for_boolean(variation) ⇒ Object

Since:

  • 6.3.0



557
558
559
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 557

def variation_for_boolean(variation)
  variation ? TRUE_VARIATION_INDEX : FALSE_VARIATION_INDEX
end

#variation_for_key(context_kind, context_key, variation) ⇒ FlagBuilder

Sets the flag to return the specified variation for a specific context 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)

    a context kind

  • context_key (String)

    a 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:

Since:

  • 6.3.0



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 171

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, variation_for_boolean(variation))
  end

  if @targets.nil?
    @targets = Hash.new
  end

  targets = @targets[context_kind] || []
  @variations.count.times do | i |
    if i == variation
      if targets[i].nil?
        targets[i] = [context_key]
      else
        targets[i].push(context_key)
      end
    elsif not targets[i].nil?
      targets[i].delete(context_key)
    end
  end

  @targets[context_kind] = targets

  self
end

#variation_for_user(user_key, variation) ⇒ FlagBuilder

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:

Since:

  • 6.3.0



215
216
217
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 215

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

#variations(*variations) ⇒ FlagBuilder

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 the desired variations

Returns:

Since:

  • 6.3.0



105
106
107
108
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 105

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