Class: Hackle::Decider

Inherits:
Object
  • Object
show all
Defined in:
lib/hackle/decision/decider.rb

Instance Method Summary collapse

Constructor Details

#initializeDecider

Returns a new instance of Decider.



31
32
33
# File 'lib/hackle/decision/decider.rb', line 31

def initialize
  @bucketer = Bucketer.new
end

Instance Method Details

#decide(experiment:, user:) ⇒ Decision

Parameters:

Returns:



39
40
41
42
43
44
45
46
47
48
# 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:) ⇒ Decision

Parameters:

Returns:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# 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