Class: Split::Trial

Inherits:
Object
  • Object
show all
Defined in:
lib/split/trial.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ Trial

Returns a new instance of Trial.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/split/trial.rb', line 9

def initialize(attrs = {})
  self.experiment   = attrs.delete(:experiment)
  self.alternative  = attrs.delete(:alternative)
  self.  = attrs.delete(:metadata)
  self.goals = attrs.delete(:goals) || []

  @user             = attrs.delete(:user)
  @options          = attrs

  @alternative_chosen = false
end

Instance Attribute Details

#experimentObject

Returns the value of attribute experiment.



6
7
8
# File 'lib/split/trial.rb', line 6

def experiment
  @experiment
end

#goalsObject

Returns the value of attribute goals.



5
6
7
# File 'lib/split/trial.rb', line 5

def goals
  @goals
end

#metadataObject



21
22
23
# File 'lib/split/trial.rb', line 21

def 
  @metadata ||= experiment.[alternative.name] if experiment.
end

Instance Method Details

#alternativeObject



25
26
27
28
29
# File 'lib/split/trial.rb', line 25

def alternative
  @alternative ||=  if @experiment.has_winner?
    @experiment.winner
  end
end

#alternative=(alternative) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/split/trial.rb', line 31

def alternative=(alternative)
  @alternative = if alternative.kind_of?(Split::Alternative)
    alternative
  else
    @experiment.alternatives.find { |a| a.name == alternative }
  end
end

#choose!(context = nil) ⇒ Object

Choose an alternative, add a participant, and save the alternative choice on the user. This method is guaranteed to only run once, and will skip the alternative choosing process if run a second time.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/split/trial.rb', line 54

def choose!(context = nil)
  @user.cleanup_old_experiments!
  # Only run the process once
  return alternative if @alternative_chosen

  new_participant = @user[@experiment.key].nil?
  if override_is_alternative?
    self.alternative = @options[:override]
    if should_store_alternative? && !@user[@experiment.key]
      self.alternative.increment_participation
    end
  elsif @options[:disabled] || Split.configuration.disabled?
    self.alternative = @experiment.control
  elsif @experiment.has_winner?
    self.alternative = @experiment.winner
  else
    cleanup_old_versions

    if exclude_user?
      self.alternative = @experiment.control
    else
      self.alternative = @user[@experiment.key]
      if alternative.nil?
        if @experiment.cohorting_disabled?
          self.alternative = @experiment.control
        else
          self.alternative = @experiment.next_alternative

          # Increment the number of participants since we are actually choosing a new alternative
          self.alternative.increment_participation

          run_callback context, Split.configuration.on_trial_choose
        end
      end
    end
  end

  new_participant_and_cohorting_disabled = new_participant && @experiment.cohorting_disabled?

  @user[@experiment.key] = alternative.name unless @experiment.has_winner? || !should_store_alternative? || new_participant_and_cohorting_disabled
  @alternative_chosen = true
  run_callback context, Split.configuration.on_trial unless @options[:disabled] || Split.configuration.disabled? || new_participant_and_cohorting_disabled
  alternative
end

#complete!(context = nil) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/split/trial.rb', line 39

def complete!(context = nil)
  if alternative
    if Array(goals).empty?
      alternative.increment_completion
    else
      Array(goals).each { |g| alternative.increment_completion(g) }
    end

    run_callback context, Split.configuration.on_trial_complete
  end
end