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_user_targets ⇒ FlagBuilder
Removes any existing user 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_not_match(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_users(value) ⇒ FlagBuilder
Sets the flag to always return the specified variation value for all users.
-
#variation_for_all_users(variation) ⇒ FlagBuilder
Sets the flag to always return the specified variation for all users.
- #variation_for_boolean(variation) ⇒ Object
-
#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
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_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.
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
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_rules ⇒ FlagBuilder
Removes any existing rules from the flag. This undoes the effect of methods like #if_match
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_targets ⇒ FlagBuilder
Removes any existing user targets from the flag. This undoes the effect of methods like #variation_for_user
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.
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.
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.
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
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.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.
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.
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.
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
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.
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.
105 106 107 108 |
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 105 def variations(*variations) @variations = variations self end |