Skip to content

Abstract Target Types

Alex Timofeev edited this page Feb 26, 2020 · 17 revisions

Available since v3.0.0.

We do our best to provide type safety between the features, rules, and what they are contextually passed during evaluation. This is a difficult problem because most applications don't have concrete types for everything. For example they don't usually have a concrete type for a user's id. Instead they use a generic type like Fixnum, Integer, UUID, etc.

Therefore, we support what we call abstract formalized target types. This is really fancy wording describing that we allow you to make up and formalize a type by simply making a symbol and using that symbol to represent that particular conceptual type.

To continue our example from above, if you wanted to formalize a user's id. You would do so by defining the target type to be :user_id. This allows you to inform Togls about the abstract formalized target type a particular feature should receive during evaluation and what abstract formalized target type a particular rule type or rule instance requires for evaluation. Once Togls has the two of these values it can enforce type safety appropriately and provide very useful feedback during the development process. For instance it will raise an exception if your feature expects a :user_id target type during evaluation and no target is passed during evaluation.

Setting the default Feature Target Type

Since v3.0.0 features toggles are required to have a abstract target type associated to them. This can be a tedious thing and often duplicative as most people stick to a small number of target types in their rules. Therefore, we have provided a default feature target type setting that allows you to define feature toggles without specifying a target type and it will default to whatever the default feature target type is set to.

The following is an example of how one would set the default feature target type, specifying that features when evaluated should not receive a target. This is usually a relatively sane default to have.

Togls.default_feature_target_type Togls::TargetTypes::NONE

Setting a Feature's Target Type

In addition to the default feature target type, you can specify a target type to be used for a particular feature rather than the default feature target type. The following is an example of how you would do this.

Togls.release do 
  feature(:my_purple_feature, 'some description', target_type: :email_address).off
end

Setting a Rule Type's Target Type

Setting a Rule Type's Target Type is done when you define the rule type (a.k.a. Custom Rule Type). You do so by defining a class level method in the Rule Type Class called target_type which should return the symbol identifying the abstract formalized type that the rule type requires for evaluation. Note: If your rule is written in a very generic fashion, similar to the Togls::Rules::Group rule, where you can't specify the actual required abstract type for evaluation. You can forgo defining the class level target_type method. This will force a target type to be given to the rule instance at construction though.

The following is an example of a Custom Rule Type that specifies a target type of :email_address.

class EmailDomainMatch < Togls::Rule
  def self.title
    "Email Domain Match"
  end

  def self.description
    "This rule type takes an email address in and determines if the target email address matches pattern given as initialization data."
  end

  def self.target_type
    :email_address
  end

  def run(feature_key, target_email_address)
    return target_email_address.ends_with?(@data)
  end
end

Setting a Rule Instance's Target Type

Some rule types are so generic they can't actually provide a meaningful abstract target type. A fine example of this is the Togls::Rules::Group rule type. In scenarios like these you are required to specify the target type at the rule instance construction because that is the point in which you actually know what the target type should be.

The following are two examples where the abstract formalized target type is being set on the rule instances. In the alpha_testers case the abstract target type of :user_id is set specifying that for this rule to properly be evaluated it requires that it be given a :user_id as the target during evaluation. In the beta_testers case the abstract target type of :user_email_address is set, specifying that for this rule to properly be evaluated it requires that it be given a :user_email_address as the target during evaluation.

alpha_testers = Togls.rule(:alpha_testers, :group, [2342, 112, 342, 523], target_type: :user_id)
beta_testers = Togls.rule(:beta_testers, :group, ['bob@example.com', 'cindy@example.com', 'jane@example.com'], target_type: :user_email_address)

Official Target Types

Togls provides the following in code representations for the official target types so that you can use them. This makes changes to the values behind these in the future more transparent if you use the in code representations.

  • Togls::TargetTypes::NONE - :none
  • Togls::TargetTypes::NOT_SET - :not_set