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: 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
(also: #clear_user_targets)
Removes any existing targets from the flag.
-
#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
-
#off_variation(variation) ⇒ FlagBuilder
Specifies the off variation for a flag.
-
#on(on) ⇒ FlagBuilder
Sets targeting to be on or off for this flag.
-
#value_for_all(value) ⇒ FlagBuilder
(also: #value_for_all_users)
Sets the flag to always return the specified variation value for all context.
-
#variation_for_all(variation) ⇒ FlagBuilder
(also: #variation_for_all_users)
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
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_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.
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
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_rules ⇒ FlagBuilder
Removes any existing rules from the flag. This undoes the effect of methods like #if_match
329 330 331 332 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 329 def clear_rules @rules = nil self end |
#clear_targets ⇒ FlagBuilder Also known as: clear_user_targets
Removes any existing targets from the flag. This undoes the effect of methods like #variation_for_key
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.
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.
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.
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.
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.
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
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.
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.
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.
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.
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
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.
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.
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.
105 106 107 108 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 105 def variations(*variations) @variations = variations self end |