Class: Toggly::Evaluators::Percentage

Inherits:
Base
  • Object
show all
Defined in:
lib/toggly/evaluators/percentage.rb

Overview

Evaluator for percentage-based rollouts.

Uses FNV-1a hash for consistent bucketing based on feature key and user identity.

Constant Summary collapse

FNV_PRIME =

FNV-1a hash constants (32-bit)

0x01000193
FNV_OFFSET_BASIS =
0x811c9dc5

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#handles?

Class Method Details

.typeObject



14
15
16
# File 'lib/toggly/evaluators/percentage.rb', line 14

def self.type
  "percentage"
end

Instance Method Details

#evaluate(rule, context, feature_key: nil) ⇒ Boolean

Evaluate percentage rollout

Parameters:

  • rule (Hash)

    Rule with "percentage" or "value" key (0-100)

  • context (Context)

    Evaluation context with identity

  • feature_key (String) (defaults to: nil)

    The feature key

Returns:

  • (Boolean)

    True if user falls within percentage



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/toggly/evaluators/percentage.rb', line 24

def evaluate(rule, context, feature_key: nil)
  percentage = rule_value(rule, "percentage") ||
               rule_value(rule, "value") ||
               0

  percentage = percentage.to_f

  # 0% always off, 100% always on
  return false if percentage <= 0
  return true if percentage >= 100

  # Need identity for percentage rollouts
  return false unless context&.identity?

  # Calculate bucket using FNV-1a hash
  bucket_key = "#{feature_key}:#{context.identity}"
  bucket = calculate_bucket(bucket_key)

  bucket < percentage
end