Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add on_winner_choose hook #574

Merged
merged 2 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,8 @@ Split.configure do |config|
# before experiment reset or deleted
config.on_before_experiment_reset = -> (example) { # Do something on reset }
config.on_before_experiment_delete = -> (experiment) { # Do something else on delete }
# after experiment winner had been set
config.on_experiment_winner_choose = -> (experiment) { # Do something on winner choose }
end
```

Expand Down
2 changes: 2 additions & 0 deletions lib/split/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class Configuration
attr_accessor :on_experiment_reset
attr_accessor :on_experiment_delete
attr_accessor :on_before_experiment_reset
attr_accessor :on_experiment_winner_choose
attr_accessor :on_before_experiment_delete
attr_accessor :include_rails_helper
attr_accessor :beta_probability_simulations
Expand Down Expand Up @@ -216,6 +217,7 @@ def initialize
@on_experiment_delete = proc{|experiment|}
@on_before_experiment_reset = proc{|experiment|}
@on_before_experiment_delete = proc{|experiment|}
@on_experiment_winner_choose = proc{|experiment|}
@db_failover_allow_parameter_override = false
@allow_multiple_experiments = false
@enabled = true
Expand Down
1 change: 1 addition & 0 deletions lib/split/experiment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ def has_winner?
def winner=(winner_name)
redis.hset(:experiment_winner, name, winner_name.to_s)
@has_winner = true
Split.configuration.on_experiment_winner_choose.call(self)
end

def participant_count
Expand Down
11 changes: 8 additions & 3 deletions spec/experiment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def alternative(color)
experiment = Split::Experiment.new('basket_text', :alternatives => ['Basket', "Cart"], :resettable => false)
expect(experiment.resettable).to be_falsey
end

context 'from configuration' do
let(:experiment_name) { :my_experiment }
let(:experiments) do
Expand All @@ -130,7 +130,7 @@ def alternative(color)
end

before { Split.configuration.experiments = experiments }

it 'assigns default values to the experiment' do
expect(Split::Experiment.new(experiment_name).resettable).to eq(true)
end
Expand Down Expand Up @@ -233,12 +233,17 @@ def alternative(color)
end

describe 'winner=' do
it "should allow you to specify a winner" do
it 'should allow you to specify a winner' do
experiment.save
experiment.winner = 'red'
expect(experiment.winner.name).to eq('red')
end

it 'should call the on_experiment_winner_choose hook' do
expect(Split.configuration.on_experiment_winner_choose).to receive(:call)
experiment.winner = 'green'
end

context 'when has_winner state is memoized' do
before { expect(experiment).to_not have_winner }

Expand Down