From 7c10dd67999d3859b607c36266d0eb80dd42b6f5 Mon Sep 17 00:00:00 2001 From: Craig McNamara Date: Wed, 7 Aug 2013 16:10:13 -0700 Subject: [PATCH 1/2] Add hooks for experiment deletion and resetting. --- lib/split/configuration.rb | 4 ++++ lib/split/experiment.rb | 2 ++ spec/experiment_spec.rb | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/lib/split/configuration.rb b/lib/split/configuration.rb index f584b9e8..563e0143 100644 --- a/lib/split/configuration.rb +++ b/lib/split/configuration.rb @@ -14,6 +14,8 @@ class Configuration attr_accessor :store_override attr_accessor :on_trial_choose attr_accessor :on_trial_complete + attr_accessor :on_experiment_reset + attr_accessor :on_experiment_delete attr_reader :experiments @@ -161,6 +163,8 @@ def initialize @ignore_filter = proc{ |request| is_robot? || is_ignored_ip_address? } @db_failover = false @db_failover_on_db_error = proc{|error|} # e.g. use Rails logger here + @on_experiment_reset = proc{|experiment|} + @on_experiment_delete = proc{|experiment|} @db_failover_allow_parameter_override = false @allow_multiple_experiments = false @enabled = true diff --git a/lib/split/experiment.rb b/lib/split/experiment.rb index 5b2a8843..f97d3667 100644 --- a/lib/split/experiment.rb +++ b/lib/split/experiment.rb @@ -210,6 +210,7 @@ def resettable? def reset alternatives.each(&:reset) reset_winner + Split.configuration.on_experiment_reset.call(self) increment_version end @@ -219,6 +220,7 @@ def delete Split.redis.srem(:experiments, name) Split.redis.del(name) delete_goals + Split.configuration.on_experiment_delete.call(self) increment_version end diff --git a/spec/experiment_spec.rb b/spec/experiment_spec.rb index 5edbb2b4..f40e5bd9 100644 --- a/spec/experiment_spec.rb +++ b/spec/experiment_spec.rb @@ -174,6 +174,11 @@ def alternative(color) experiment.delete experiment.version.should eql(1) end + + it "should call the on_experiment_delete hook" do + expect(Split.configuration.on_experiment_delete).to receive(:call) + experiment.delete + end end @@ -219,6 +224,11 @@ def alternative(color) experiment.reset experiment.version.should eql(1) end + + it "should call the on_experiment_reset hook" do + expect(Split.configuration.on_experiment_reset).to receive(:call) + experiment.reset + end end describe 'algorithm' do From 791aaef3d611390bf730ab59a612b8090e9df88a Mon Sep 17 00:00:00 2001 From: Craig McNamara Date: Thu, 8 Aug 2013 02:50:36 -0700 Subject: [PATCH 2/2] Add some documentation for experiment hooks. --- README.mdown | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.mdown b/README.mdown index 1488d006..c797fcaf 100644 --- a/README.mdown +++ b/README.mdown @@ -247,6 +247,19 @@ def log_trial_complete(trial) end ``` +### Experiment Hooks + +You can assign a proc that will be called when an experiment is reset or deleted. You can use these hooks to call methods within your application to keep data related to experiments in sync with Split. + +For example: + +``` ruby +Split.configure do |config| + config.on_experiment_reset = proc{ |experiment| # Do something on reset } + config.on_experiment_delete = proc{ |experiment| # Do something else on delete } +end +``` + ## Web Interface Split comes with a Sinatra-based front end to get an overview of how your experiments are doing.