Class: LaunchDarkly::Integrations::TestDataV2::FlagRuleBuilderV2

Inherits:
Object
  • Object
show all
Defined in:
lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb

Overview

A builder for feature flag rules to be used with FlagBuilderV2.

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 LaunchDarkly::Integrations::TestDataV2::FlagBuilderV2#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.

Instance Method Summary collapse

Constructor Details

#initialize(flag_builder) ⇒ FlagRuleBuilderV2

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 FlagRuleBuilderV2.

Parameters:



434
435
436
437
438
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 434

def initialize(flag_builder)
  @_flag_builder = flag_builder
  @_clauses = []
  @_variation = nil
end

Instance Method Details

#and_match(attribute, *values) ⇒ FlagRuleBuilderV2

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”

td.flag('flag')
    .if_match('name', 'Patsy')
    .and_match('country', 'gb')
    .then_return(true)

Parameters:

  • attribute (String)

    the user attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:



462
463
464
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 462

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

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

Adds another clause, using the “is one of” operator. This matching expression only applies to contexts of a specific kind.

Examples:

create a rule that returns true if the name attribute for the

"company" context is "Ella", and the country attribute for the "company" context is "gb":
td.flag('flag')
    .if_match_context('company', 'name', 'Ella')
    .and_match_context('company', 'country', 'gb')
    .then_return(true)

Parameters:

  • context_kind (String)

    the context kind

  • attribute (String)

    the context attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:



482
483
484
485
486
487
488
489
490
491
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 482

def and_match_context(context_kind, attribute, *values)
  @_clauses << {
    contextKind: context_kind,
    attribute: attribute,
    op: 'in',
    values: values.to_a,
    negate: false,
  }
  self
end

#and_not_match(attribute, *values) ⇒ FlagRuleBuilderV2

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

This is a shortcut for calling #and_not_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 not “gb”

td.flag('flag')
    .if_match('name', 'Patsy')
    .and_not_match('country', 'gb')
    .then_return(true)

Parameters:

  • attribute (String)

    the user attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:



509
510
511
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 509

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) ⇒ FlagRuleBuilderV2

Adds another clause, using the “is not one of” operator. This matching expression only applies to contexts of a specific kind.

Examples:

create a rule that returns true if the name attribute for the

"company" context is "Ella", and the country attribute for the "company" context is not "gb":
td.flag('flag')
    .if_match_context('company', 'name', 'Ella')
    .and_not_match_context('company', 'country', 'gb')
    .then_return(true)

Parameters:

  • context_kind (String)

    the context kind

  • attribute (String)

    the context attribute to match against

  • values (Array<Object>)

    values to compare to

Returns:



529
530
531
532
533
534
535
536
537
538
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 529

def and_not_match_context(context_kind, attribute, *values)
  @_clauses << {
    contextKind: context_kind,
    attribute: attribute,
    op: 'in',
    values: values.to_a,
    negate: true,
  }
  self
end

#build(id) ⇒ 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 FlagBuilderV2.

Creates a dictionary representation of the rule

Parameters:

  • id (String)

    the rule id

Returns:

  • (Hash)

    the dictionary representation of the rule



571
572
573
574
575
576
577
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 571

def build(id)
  {
    id: 'rule' + id,
    variation: @_variation,
    clauses: @_clauses,
  }
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.



441
442
443
444
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 441

def initialize_copy(other)
  super(other)
  @_clauses = @_clauses.map(&:clone)
end

#then_return(variation) ⇒ FlagBuilderV2

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:



551
552
553
554
555
556
557
558
559
560
# File 'lib/ldclient-rb/integrations/test_data_v2/flag_builder_v2.rb', line 551

def then_return(variation)
  if LaunchDarkly::Impl::Util.bool?(variation)
    @_flag_builder.boolean_flag
    return then_return(TestDataV2.variation_for_boolean(variation))
  end

  @_variation = variation
  @_flag_builder.add_rule(self)
  @_flag_builder
end