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



245
246
247
248
249
250
251
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 245

def add_rule(rule)
  if @rules.nil? then
    @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



263
264
265
266
267
268
269
270
271
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 263

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

#build(version) ⇒ Object

Since:

  • 6.3.0



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 274

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

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

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

  unless @targets.nil? then
    res[:targets] = @targets.collect do | variation, values |
      { variation: variation, values: values }
    end
  end

  unless @rules.nil? then
    res[:rules] = @rules.each_with_index.collect { | 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



239
240
241
242
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 239

def clear_rules
  @rules = nil
  self
end

#clear_user_targetsFlagBuilder

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

Returns:

Since:

  • 6.3.0



228
229
230
231
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 228

def clear_user_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 user 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.is_bool variation then
    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.

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



198
199
200
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 198

def if_match(attribute, *values)
  FlagRuleBuilder.new(self).and_match(attribute, *values)
end

#if_not_match(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(: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



218
219
220
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 218

def if_not_match(attribute, *values)
  FlagRuleBuilder.new(self).and_not_match(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.is_bool variation then
    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_users(value) ⇒ FlagBuilder

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

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 users

Returns:

Since:

  • 6.3.0



142
143
144
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 142

def value_for_all_users(value)
  variations(value).variation_for_all_users(0)
end

#variation_for_all_users(variation) ⇒ FlagBuilder

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

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_users(variation)
  if LaunchDarkly::Impl::Util.is_bool variation then
    boolean_flag.variation_for_all_users(variation_for_boolean(variation))
  else
    on(true).clear_rules.clear_user_targets.fallthrough_variation(variation)
  end
end

#variation_for_boolean(variation) ⇒ Object

Since:

  • 6.3.0



409
410
411
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 409

def variation_for_boolean(variation)
  variation ? TRUE_VARIATION_INDEX : FALSE_VARIATION_INDEX
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 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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 160

def variation_for_user(user_key, variation)
  if LaunchDarkly::Impl::Util.is_bool variation then
    boolean_flag.variation_for_user(user_key, variation_for_boolean(variation))
  else
    if @targets.nil? then
      @targets = Hash.new
    end
    @variations.count.times do | i |
      if i == variation then
        if @targets[i].nil? then
          @targets[i] = [user_key]
        else
          @targets[i].push(user_key)
        end
      elsif not @targets[i].nil? then
        @targets[i].delete(user_key)
      end
    end
    self
  end
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