class Hackle::Decider

Public Class Methods

new() click to toggle source
# File lib/hackle/decision/decider.rb, line 31
def initialize
  @bucketer = Bucketer.new
end

Public Instance Methods

decide(experiment:, user:) click to toggle source

@param experiment [Experiment] @param user [User]

@return [Decision]

# File lib/hackle/decision/decider.rb, line 39
def decide(experiment:, user:)
  case experiment
  when Experiment::Completed
    Decision::ForcedAllocated.new(variation_key: experiment.winner_variation_key)
  when Experiment::Running
    decide_running(running_experiment: experiment, user: user)
  else
    NotAllocated.new
  end
end
decide_running(running_experiment:, user:) click to toggle source

@param running_experiment [Experiment::Running] @param user [User]

@return [Decision]

# File lib/hackle/decision/decider.rb, line 54
def decide_running(running_experiment:, user:)

  overridden_variation = running_experiment.get_overridden_variation(user: user)
  return Decision::ForcedAllocated.new(variation_key: overridden_variation.key) unless overridden_variation.nil?

  allocated_slot = @bucketer.bucketing(bucket: running_experiment.bucket, user: user)
  return Decision::NotAllocated.new if allocated_slot.nil?

  allocated_variation = running_experiment.get_variation(variation_id: allocated_slot.variation_id)
  return Decision::NotAllocated.new if allocated_variation.nil?
  return Decision::NotAllocated.new if allocated_variation.dropped

  Decision::NaturalAllocated.new(variation: allocated_variation)
end