Class: LaunchDarkly::Integrations::TestData::FlagBuilder
- Inherits:
-
Object
- Object
- LaunchDarkly::Integrations::TestData::FlagBuilder
- 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.
Defined Under Namespace
Classes: FlagMigrationSettingsBuilder, FlagRuleBuilder
Instance Attribute Summary collapse
- #key ⇒ Object readonly
Instance Method Summary collapse
- #add_rule(rule) ⇒ Object
-
#boolean_flag ⇒ FlagBuilder
A shortcut for setting the flag to use the standard boolean configuration.
- #build(version) ⇒ Object
-
#clear_rules ⇒ FlagBuilder
Removes any existing rules from the flag.
-
#clear_targets ⇒ FlagBuilder
Removes any existing targets from the flag.
-
#exclude_from_summaries(exclude) ⇒ FlagBuilder
Set the option to exclude this flag from summary events.
-
#fallthrough_variation(variation) ⇒ FlagBuilder
Specifies the fallthrough variation.
-
#if_match(attribute, *values) ⇒ FlagRuleBuilder
Starts defining a flag rule, using the “is one of” operator.
-
#if_match_context(context_kind, attribute, *values) ⇒ FlagRuleBuilder
Starts defining a flag rule, using the “is one of” operator.
-
#if_not_match(attribute, *values) ⇒ FlagRuleBuilder
Starts defining a flag rule, using the “is not one of” operator.
-
#if_not_match_context(context_kind, attribute, *values) ⇒ FlagRuleBuilder
Starts defining a flag rule, using the “is not one of” operator.
-
#initialize(key) ⇒ FlagBuilder
constructor
A new instance of FlagBuilder.
- #initialize_copy(other) ⇒ Object
-
#migration_settings(settings) ⇒ FlagBuilder
Set the migration related settings for this feature flag.
-
#off_variation(variation) ⇒ FlagBuilder
Specifies the off variation for a flag.
-
#on(on) ⇒ FlagBuilder
Sets targeting to be on or off for this flag.
-
#sampling_ratio(ratio) ⇒ FlagBuilder
Set the sampling ratio for this flag.
-
#value_for_all(value) ⇒ FlagBuilder
Sets the flag to always return the specified variation value for all context.
-
#variation_for_all(variation) ⇒ FlagBuilder
Sets the flag to always return the specified variation for all contexts.
- #variation_for_boolean(variation) ⇒ Object
-
#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.
-
#variation_for_user(user_key, variation) ⇒ FlagBuilder
Sets the flag to return the specified variation for a specific user key when targeting is on.
-
#variations(*variations) ⇒ FlagBuilder
Changes the allowable variation values for the flag.
Constructor Details
#initialize(key) ⇒ FlagBuilder
Returns a new instance of FlagBuilder.
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
#key ⇒ Object (readonly)
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
361 362 363 364 365 366 367 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 361 def add_rule(rule) if @rules.nil? @rules = Array.new end @rules.push(rule) self end |
#boolean_flag ⇒ FlagBuilder
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.
379 380 381 382 383 384 385 386 387 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 379 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
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 390 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 @migration_settings.nil? res[:migration] = @migration_settings end unless @sampling_ratio.nil? || @sampling_ratio == 1 res[:samplingRatio] = @sampling_ratio end unless @exclude_from_summaries.nil? || !@exclude_from_summaries res[:excludeFromSummaries] = @exclude_from_summaries 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_rules ⇒ FlagBuilder
Removes any existing rules from the flag. This undoes the effect of methods like #if_match
355 356 357 358 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 355 def clear_rules @rules = nil self end |
#clear_targets ⇒ FlagBuilder
Removes any existing targets from the flag. This undoes the effect of methods like #variation_for_key
344 345 346 347 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 344 def clear_targets @targets = nil self end |
#exclude_from_summaries(exclude) ⇒ FlagBuilder
Set the option to exclude this flag from summary events. This is used to control the size of the summary event in the event certain flag payloads are large.
General usage should not require interacting with this method.
82 83 84 85 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 82 def exclude_from_summaries(exclude) @exclude_from_summaries = exclude 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.
99 100 101 102 103 104 105 106 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 99 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.
290 291 292 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 290 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.
267 268 269 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 267 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.
334 335 336 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 334 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.
311 312 313 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 311 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
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 |
#migration_settings(settings) ⇒ FlagBuilder
Set the migration related settings for this feature flag.
The settings hash should be built using the FlagMigrationSettingsBuilder.
54 55 56 57 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 54 def migration_settings(settings) @migration_settings = settings self 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.
119 120 121 122 123 124 125 126 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 119 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.
41 42 43 44 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 41 def on(on) @on = on self end |
#sampling_ratio(ratio) ⇒ FlagBuilder
Set the sampling ratio for this flag. This ratio is used to control the emission rate of feature, debug, and migration op events.
General usage should not require interacting with this method.
68 69 70 71 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 68 def sampling_ratio(ratio) @sampling_ratio = ratio self end |
#value_for_all(value) ⇒ FlagBuilder
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.
183 184 185 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 183 def value_for_all(value) variations(value).variation_for_all(0) end |
#variation_for_all(variation) ⇒ FlagBuilder
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.
164 165 166 167 168 169 170 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 164 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
626 627 628 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 626 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.
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 202 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.
246 247 248 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 246 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.
146 147 148 149 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 146 def variations(*variations) @variations = variations self end |