Class: LaunchDarkly::Integrations::TestDataV2::FlagBuilderV2
- Inherits:
-
Object
- Object
- LaunchDarkly::Integrations::TestDataV2::FlagBuilderV2
- 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.
Instance Attribute Summary collapse
- #_key ⇒ Object readonly private
Instance Method Summary collapse
- #add_rule(flag_rule_builder) ⇒ Object private
-
#boolean_flag ⇒ FlagBuilderV2
A shortcut for setting the flag to use the standard boolean configuration.
-
#build(version) ⇒ Hash
private
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.
-
#clear_rules ⇒ FlagBuilderV2
Removes any existing rules from the flag.
-
#clear_targets ⇒ FlagBuilderV2
Removes any existing targets from the flag.
-
#fallthrough_variation(variation) ⇒ FlagBuilderV2
Specifies the fallthrough variation.
-
#if_match(attribute, *values) ⇒ FlagRuleBuilderV2
Starts defining a flag rule, using the “is one of” operator.
-
#if_match_context(context_kind, attribute, *values) ⇒ FlagRuleBuilderV2
Starts defining a flag rule, using the “is one of” operator.
-
#if_not_match(attribute, *values) ⇒ FlagRuleBuilderV2
Starts defining a flag rule, using the “is not one of” operator.
-
#if_not_match_context(context_kind, attribute, *values) ⇒ FlagRuleBuilderV2
Starts defining a flag rule, using the “is not one of” operator.
-
#initialize(key) ⇒ FlagBuilderV2
constructor
private
A new instance of FlagBuilderV2.
-
#initialize_copy(other) ⇒ Object
private
Creates a deep copy of the flag builder when the object is duplicated or cloned.
-
#off_variation(variation) ⇒ FlagBuilderV2
Specifies the off variation.
-
#on(on) ⇒ FlagBuilderV2
Sets targeting to be on or off for this flag.
-
#value_for_all(value) ⇒ FlagBuilderV2
Sets the flag to always return the specified variation value for all contexts.
-
#variation_for_all(variation) ⇒ FlagBuilderV2
Sets the flag to always return the specified variation for all contexts.
-
#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.
-
#variation_for_user(user_key, variation) ⇒ FlagBuilderV2
Sets the flag to return the specified variation for a specific user key when targeting is on.
-
#variations(*variations) ⇒ FlagBuilderV2
Changes the allowable variation values for the flag.
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
#_key ⇒ Object (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_flag ⇒ FlagBuilderV2
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.
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
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_rules ⇒ FlagBuilderV2
Removes any existing rules from the flag. This undoes the effect of methods like #if_match.
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_targets ⇒ FlagBuilderV2
Removes any existing targets from the flag. This undoes the effect of methods like #variation_for_user.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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 |