Class: Metry::Experiment

Inherits:
Object
  • Object
show all
Includes:
MongoMapper::Document
Defined in:
lib/metry/experiment.rb

Constant Summary collapse

METHODS =
{
  "rand" => proc{|list, experiment| list.sort_by{rand}.first},
  "sequential" => proc do |list, experiment|
    r = list[experiment.counter%list.size]
    experiment.counter += 1
    experiment.save
    r
  end
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_or_create_by_name(name) ⇒ Object



20
21
22
# File 'lib/metry/experiment.rb', line 20

def self.find_or_create_by_name(name)
  find(:first, :conditions => {:name => name}) || create(:name => name)
end

Instance Method Details

#choose(options, method, event, visitor) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/metry/experiment.rb', line 37

def choose(options, method, event, visitor)
  cohort = cohort_for(visitor)
  unless cohort
    cohort = lookup_cohort(METHODS[method||'rand'][options, self])
    visitor.in_cohort(cohort)
  end
  cohort.name
end

#cohort_for(visitor) ⇒ Object



33
34
35
# File 'lib/metry/experiment.rb', line 33

def cohort_for(visitor)
  cohorts.detect{|e| e.visitor_ids.include?(visitor.id)}
end

#lookup_cohort(name) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/metry/experiment.rb', line 24

def lookup_cohort(name)
  cohort = cohorts.find(:first, :conditions => {:name => name})
  unless cohort
    cohort = Cohort.create(:name => name)
    cohorts << cohort
  end
  cohort
end