Class: LaunchDarkly::Integrations::TestData::FlagBuilder::FlagRuleBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/integrations/test_data/flag_builder.rb

Overview

A builder for feature flag rules to be used with LaunchDarkly::Integrations::TestData::FlagBuilder.

In the LaunchDarkly model, a flag can have any number of rules, and a rule can have any number of clauses. A clause is an individual test such as "name is 'X'". A rule matches a context if all of the rule's clauses match the context.

To start defining a rule, use one of the flag builder's matching methods such as #if_match. This defines the first clause for the rule. Optionally, you may add more clauses with the rule builder's methods such as #and_match or #and_not_match. Finally, call #then_return to finish defining the rule.

Since:

  • 6.3.0

Instance Method Summary collapse

Instance Method Details

#and_match(attribute, *values) ⇒ FlagRuleBuilder

Adds another clause, using the "is one of" operator.

This is a shortcut for calling #and_match_context with LaunchDarkly::LDContext::KIND_DEFAULT as the context kind.

Examples:

create a rule that returns true if the name is "Patsy" and the country is "gb"

testData.flag("flag")
    .if_match(:name, 'Patsy')
    .and_match(:country, 'gb')
    .then_return(true)

Parameters:

  • attribute (Symbol)

    the user attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:

Since:

  • 6.3.0



545
546
547
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 545

def and_match(attribute, *values)
  and_match_context(LaunchDarkly::LDContext::KIND_DEFAULT, attribute, *values)
end

#and_match_context(context_kind, attribute, *values) ⇒ FlagRuleBuilder

Adds another clause, using the "is one of" operator.

Examples:

create a rule that returns true if the name is "Patsy", the country is "gb", and the context kind is "user"

testData.flag("flag")
    .if_match_context("user", :name, 'Patsy')
    .and_match_context("user", :country, 'gb')
    .then_return(true)

Parameters:

  • context_kind (String)

    a context kind

  • attribute (Symbol)

    the context attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:

Since:

  • 6.3.0



518
519
520
521
522
523
524
525
526
527
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 518

def and_match_context(context_kind, attribute, *values)
  @clauses.push(FlagRuleClause.new(
    contextKind: context_kind,
    attribute: attribute,
    op: 'in',
    values: values,
    negate: false
  ))
  self
end

#and_not_match(attribute, *values) ⇒ FlagRuleBuilder

Adds another clause, using the "is not one of" operator.

This is a shortcut for calling #and_not_match with LaunchDarkly::LDContext::KIND_DEFAULT as the context kind.

Examples:

create a rule that returns true if the name is "Patsy" and the country is not "gb"

testData.flag("flag")
    .if_match(:name, 'Patsy')
    .and_not_match(:country, 'gb')
    .then_return(true)

Parameters:

  • attribute (Symbol)

    the user attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:

Since:

  • 6.3.0



590
591
592
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 590

def and_not_match(attribute, *values)
  and_not_match_context(LaunchDarkly::LDContext::KIND_DEFAULT, attribute, *values)
end

#and_not_match_context(context_kind, attribute, *values) ⇒ FlagRuleBuilder

Adds another clause, using the "is not one of" operator.

Examples:

create a rule that returns true if the name is "Patsy" and the country is not "gb"

testData.flag("flag")
    .if_match_context("user", :name, 'Patsy')
    .and_not_match_context("user", :country, 'gb')
    .then_return(true)

Parameters:

  • context_kind (String)

    a context kind

  • attribute (Symbol)

    the context attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:

Since:

  • 6.3.0



563
564
565
566
567
568
569
570
571
572
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 563

def and_not_match_context(context_kind, attribute, *values)
  @clauses.push(FlagRuleClause.new(
    contextKind: context_kind,
    attribute: attribute,
    op: 'in',
    values: values,
    negate: true
  ))
  self
end

#then_return(variation) ⇒ FlagBuilder

Finishes defining the rule, specifying the result as either a boolean or a variation index.

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: 0 for the first, 1 for the second, etc.

Returns:

  • (FlagBuilder)

    the flag builder with this rule added

Since:

  • 6.3.0



605
606
607
608
609
610
611
612
613
# File 'lib/ldclient-rb/integrations/test_data/flag_builder.rb', line 605

def then_return(variation)
  if LaunchDarkly::Impl::Util.bool? variation
    @variation = @flag_builder.variation_for_boolean(variation)
    @flag_builder.boolean_flag.add_rule(self)
  else
    @variation = variation
    @flag_builder.add_rule(self)
  end
end