From 7ed033f673a4a6f92bbf96dbf463764e44e560f3 Mon Sep 17 00:00:00 2001 From: Daniel Dao Date: Sun, 19 Sep 2021 23:25:14 -0700 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Dont=20set=20default=20experiment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/scientist/experiment.rb | 7 ++++++- test/scientist/experiment_test.rb | 13 +++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/lib/scientist/experiment.rb b/lib/scientist/experiment.rb index dbffd67..fd1c5ef 100644 --- a/lib/scientist/experiment.rb +++ b/lib/scientist/experiment.rb @@ -10,10 +10,15 @@ module Scientist::Experiment attr_accessor :raise_on_mismatches def self.included(base) - self.set_default(base) if base.instance_of?(Class) + set_default(base) if base.instance_of?(Class) base.extend RaiseOnMismatch end + # Set this class as default scientist experiment when included. + def self.set_as_default_scientist_experiment(set_default_class) + set_default(Scientist::Default) unless set_default_class + end + # Instantiate a new experiment (using the class given to the .set_default method). def self.new(name) (@experiment_klass || Scientist::Default).new(name) diff --git a/test/scientist/experiment_test.rb b/test/scientist/experiment_test.rb index 72f5207..bea95ca 100644 --- a/test/scientist/experiment_test.rb +++ b/test/scientist/experiment_test.rb @@ -31,6 +31,19 @@ def publish(result) @ex = Fake.new end + it "does not set as default when default_scientist_experiment is passed as false" do + klass = Class.new do + include Scientist::Experiment + + Scientist::Experiment.set_as_default_scientist_experiment(false) + + def initialize(name) + end + end + + assert_kind_of Scientist::Default, Scientist::Experiment.new("hello") + end + it "sets the default on inclusion" do klass = Class.new do include Scientist::Experiment