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



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

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



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

def experiment
  @experiment
end

#metadataObject

Returns the value of attribute metadata



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

def 
  @metadata
end

Instance Method Details

#alternativeObject



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

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

#alternative=(alternative) ⇒ Object



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

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)
  # Only run the process once
  return alternative if @alternative_choosen

  if @options[:override]
    self.alternative = @options[:override]
  elsif @options[:disabled] || !Split.configuration.enabled
    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 the post-choosing hook on the context
      context.send(Split.configuration.on_trial_choose, self) \
          if Split.configuration.on_trial_choose && context
    end
  end

  @user[@experiment.key] = alternative.name if should_store_alternative?
  @alternative_choosen = true
  alternative
end

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



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

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

    context.send(Split.configuration.on_trial_complete, self) \
        if Split.configuration.on_trial_complete && context
  end
end