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.



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

def initialize(attrs = {})
  self.experiment = attrs[:experiment]  if !attrs[:experiment].nil?
  self.alternative = attrs[:alternative] if !attrs[:alternative].nil?
  self.goals = attrs[:goals].nil? ? [] : attrs[:goals]
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

#goalsObject

Returns the value of attribute goals.



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

def goals
  @goals
end

Instance Method Details

#alternativeObject



12
13
14
15
16
# File 'lib/split/trial.rb', line 12

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

#alternative=(alternative) ⇒ Object



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

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

#chooseObject



37
38
39
# File 'lib/split/trial.rb', line 37

def choose
  self.alternative = experiment.next_alternative
end

#choose!Object



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

def choose!
  choose
  record!
end

#complete!Object



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

def complete!
  if alternative
    if self.goals.empty?
      alternative.increment_completion
    else
      self.goals.each {|g| alternative.increment_completion(g)}
    end
  end
end

#record!Object



33
34
35
# File 'lib/split/trial.rb', line 33

def record!
  alternative.increment_participation
end