Navigation Menu

Skip to content

Commit

Permalink
revamping of error raising.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jay Adkisson committed May 5, 2010
1 parent 19e2ce9 commit b8bd65a
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 19 deletions.
14 changes: 10 additions & 4 deletions lib/modesty/experiment.rb
Expand Up @@ -5,11 +5,17 @@ class Error < StandardError; end

module ExperimentMethods
def experiments
@experiments ||= {}
@experiments ||= Hash.new do |h, k|
raise Experiment::Error, <<-msg.squish
Unrecognized experiment #{k.inspect}.
msg
end
end

def add_experiment(exp)
raise Error, "Experiment already defined!" if self.experiments[exp.slug]
raise Experiment::Error, <<-msg if self.experiments.include? exp.slug
Experiment #{exp.slug.inspect} already defined!
msg
self.experiments[exp.slug] = exp
end

Expand Down Expand Up @@ -38,8 +44,8 @@ def decide_identity(options)
end
end

def experiment(exp, options={}, &blk)
exp = self.experiments[exp]
def experiment(sym, options={}, &blk)
exp = self.experiments[sym]

identity = decide_identity(options)

Expand Down
18 changes: 9 additions & 9 deletions lib/modesty/metric.rb
Expand Up @@ -7,13 +7,17 @@ module MetricMethods
attr_writer :metrics

def metrics
@metrics ||= {}
@metrics ||= Hash.new do |h, k|
raise Metric::Error, <<-msg.squish
Unrecognized metric #{k.inspect}
msg
end
end

def add_metric(metric)
if self.metrics[metric.slug]
raise "Metric #{metric.slug.inspect} already defined!"
end
raise Metric::Error <<-msg if self.metrics.include? metric.slug
Metric #{metric.slug.inspect} already defined!
msg
self.metrics[metric.slug] = metric
end

Expand All @@ -26,11 +30,7 @@ def new_metric(slug, parent=nil, &block)

#Tracking
def track!(sym, *args)
if self.metrics.include? sym
self.metrics[sym].track! *args
else
raise Metric::Error, "Unrecognized metric #{sym.inspect}"
end
self.metrics[sym].track! *args
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/experiment_spec.rb
Expand Up @@ -86,6 +86,12 @@
end
end

it "raises Modesty::Experiment::MissingError if exp is missing" do
lambda do
Modesty.group(:idontexist)
end.should raise_error(Modesty::Experiment::Error)
end

it "assigns guests to :control" do
Modesty.identify! nil
Modesty.group(:ab_test).should == :control
Expand Down
8 changes: 2 additions & 6 deletions spec/metric_spec.rb
Expand Up @@ -6,7 +6,7 @@
end

before :each do
Modesty.metrics = {}
Modesty.metrics.clear
end

it "can create a metric without a block" do
Expand Down Expand Up @@ -47,10 +47,6 @@
Modesty.metrics[:foo/:bar/:baz].parent.should_not == nil
Modesty.metrics[:foo/:bar/:baz].parent.slug.should == :foo/:bar
end

after :all do
Modesty.metrics = {}
end
end

describe Modesty::Metric, "Tracking Metrics" do
Expand All @@ -60,7 +56,7 @@

before :all do
Modesty.set_store :redis, :mock => true
Modesty.metrics = {}
Modesty.metrics.clear
Modesty.new_metric :foo do |foo|
foo.description "Foo"

Expand Down

0 comments on commit b8bd65a

Please sign in to comment.