Skip to content

Commit

Permalink
grab experiment-group metrics nicely with exp.metrics(:group)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Adkisson committed May 3, 2010
1 parent 832df37 commit 740de0b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 4 deletions.
20 changes: 16 additions & 4 deletions lib/modesty/experiment.rb
@@ -1,5 +1,7 @@
module Modesty
class Experiment
class Error < StandardError; end

class Builder
def method_missing(name, *args)
if Experiment::ATTRIBUTES.include?(name) && args.count > 0
Expand Down Expand Up @@ -69,8 +71,17 @@ def alternatives
@alternatives ||= [:control, :experiment]
end

def metrics
def metrics(alt=nil)
@metrics ||= []
return @metrics unless alt
raise Error, <<-msg.squish unless self.alternatives.include? alt
Unrecognized alternative #{alt.inspect} for #{self.inspect}.
Available alternatives: #{self.alternatives.inspect}
msg

Hash[@metrics.map do |m|
[m.slug, m/(self.slug/alt)]
end]
end

def data
Expand Down Expand Up @@ -132,11 +143,12 @@ def users(alt=nil)
end

module ExperimentMethods
attr_accessor :experiments
def experiments
@experiments ||= {}
end

def add_experiment(exp)
@experiments ||= {}
raise "Experiment already defined!" if @experiments[exp.slug]
raise Error, "Experiment already defined!" if @experiments[exp.slug]
@experiments[exp.slug] = exp
end

Expand Down
13 changes: 13 additions & 0 deletions spec/experiment_spec.rb
Expand Up @@ -52,6 +52,19 @@
Modesty.metrics.keys.should include :baz/:ab_test/:control
Modesty.metrics.keys.should include :baz/:ab_test/:experiment
end

it "grabs metrics nicely with e.metrics(alt)" do
@e.alternatives.each do |alt|
@e.metrics(alt).should == {
:foo/:bar => Modesty.metrics[:foo/:bar/:creation_page/alt],
:baz => Modesty.metrics[:baz/:creation_page/alt],
}
end

lambda do
@e.metrics(:i_dont_exist)
end.should raise_error(Modesty::Experiment::Error)
end
end

describe "A/B testing" do
Expand Down

0 comments on commit 740de0b

Please sign in to comment.