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


7
8
9
10
11
12
13
14
15
16
# File 'lib/split/trial.rb', line 7

def initialize(attrs = {})
  self.experiment   = attrs.delete(:experiment)
  self.alternative  = attrs.delete(:alternative)
  self.  = attrs.delete(:metadata)

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

  @alternative_choosen = false
end

Instance Attribute Details

#experimentObject

Returns the value of attribute experiment


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

def experiment
  @experiment
end

#metadataObject

Returns the value of attribute metadata


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

def 
  @metadata
end

Instance Method Details

#alternativeObject


22
23
24
25
26
# File 'lib/split/trial.rb', line 22

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

#alternative=(alternative) ⇒ Object


28
29
30
31
32
33
34
# File 'lib/split/trial.rb', line 28

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.


51
52
53
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
# File 'lib/split/trial.rb', line 51

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

  if override_is_alternative?
    self.alternative = @options[:override]
  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
    elsif @user[@experiment.key]
      self.alternative = @user[@experiment.key]
    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

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

#complete!(goals = [], context = nil) ⇒ Object


36
37
38
39
40
41
42
43
44
45
46
# File 'lib/split/trial.rb', line 36

def complete!(goals=[], 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