lib/vanity/experiment/ab_test.rb in vanity-2.1.2 vs lib/vanity/experiment/ab_test.rb in vanity-2.2.0

- old
+ new

@@ -25,14 +25,14 @@ super @score_method = DEFAULT_SCORE_METHOD @use_probabilities = nil @is_default_set = false end - + # -- Default -- - # Call this method once to set a default alternative. Call without + # Call this method once to set a default alternative. Call without # arguments to obtain the current default. If default is not specified, # the first alternative is used. # # @example Set the default alternative # ab_test "Background color" do @@ -53,27 +53,29 @@ end nil end # -- Enabled -- - + # Returns true if experiment is enabled, false if disabled. def enabled? !@playground.collecting? || (active? && connection.is_experiment_enabled?(@id)) end - + # Enable or disable the experiment. Only works if the playground is collecting # and this experiment is enabled. # # **Note** You should *not* set the enabled/disabled status of an # experiment until it exists in the database. Ensure that your experiment # has had #save invoked previous to any enabled= calls. def enabled=(bool) return unless @playground.collecting? && active? if created_at.nil? - warn 'DB has no created_at for this experiment! This most likely means' + - 'you didn\'t call #save before calling enabled=, which you should.' + Vanity.logger.warn( + 'DB has no created_at for this experiment! This most likely means' + + 'you didn\'t call #save before calling enabled=, which you should.' + ) else connection.set_experiment_enabled(@id, bool) end end @@ -185,13 +187,13 @@ # color = experiment(:which_blue).choose def choose(request=nil) if @playground.collecting? if active? if enabled? - return assignment_for_identity(request) + return assignment_for_identity(request) else - # Show the default if experiment is disabled. + # Show the default if experiment is disabled. return default end else # If inactive, always show the outcome. Fallback to generation if one can't be found. index = connection.ab_get_outcome(@id) || alternative_for(identity) @@ -338,17 +340,17 @@ begin require "backports/1.9.1/kernel/define_singleton_method" if RUBY_VERSION < "1.9" require "integration" require "rubystats" rescue LoadError - fail "to use bayes_bandit_score, install integration and rubystats gems" + fail("to use bayes_bandit_score, install integration and rubystats gems") end begin require "gsl" rescue LoadError - warn "for better integration performance, install gsl gem" + Vanity.logger.warn("for better integration performance, install gsl gem") end BayesianBanditScore.new(alternatives, outcome).calculate! end @@ -478,12 +480,12 @@ unless outcome if @outcome_is begin result = @outcome_is.call outcome = result.id if Alternative === result && result.experiment == self - rescue - warn "Error in AbTest#complete!: #{$!}" + rescue => e + Vanity.logger.warn("Error in AbTest#complete!: #{e}") end else best = score.best outcome = best.id if best end @@ -497,11 +499,11 @@ def destroy connection.destroy_experiment(@id) super end - + # clears all collected data for the experiment def reset return unless @playground.collecting? connection.destroy_experiment(@id) connection.set_experiment_created_at(@id, Time.now) @@ -522,33 +524,33 @@ # If collecting, this method will also store this experiment into the db. # In most cases, you call this method right after the experiment's been instantiated # and declared. def save if @saved - warn "Experiment #{name} has already been saved" + Vanity.logger.warn("Experiment #{name} has already been saved") return end @saved = true true_false unless @alternatives fail "Experiment #{name} needs at least two alternatives" unless @alternatives.size >= 2 if !@is_default_set default(@alternatives.first) - warn "No default alternative specified; choosing #{@default} as default." + Vanity.logger.warn("No default alternative specified; choosing #{@default} as default.") elsif alternative(@default).nil? - #Specified a default that wasn't listed as an alternative; warn and override. - warn "Attempted to set unknown alternative #{@default} as default! Using #{@alternatives.first} instead." - #Set the instance variable directly since default(value) is no longer defined + # Specified a default that wasn't listed as an alternative; warn and override. + Vanity.logger.warn("Attempted to set unknown alternative #{@default} as default! Using #{@alternatives.first} instead.") + # Set the instance variable directly since default(value) is no longer defined @default = @alternatives.first end super if @metrics.nil? || @metrics.empty? - warn "Please use metrics method to explicitly state which metric you are measuring against." + Vanity.logger.warn("Please use metrics method to explicitly state which metric you are measuring against.") metric = @playground.metrics[id] ||= Vanity::Metric.new(@playground, name) @metrics = [metric] end @metrics.each do |metric| - metric.hook &method(:track!) + metric.hook(&method(:track!)) end end # Called via a hook by the associated metric. def track!(metric_id, timestamp, count, *args) @@ -585,11 +587,11 @@ end end end # Returns the assigned alternative, previously chosen alternative, or - # alternative_for for a given identity. + # alternative_for for a given identity. def assignment_for_identity(request) identity = identity() if filter_visitor?(request, identity) default else @@ -631,10 +633,10 @@ connection.ab_add_participant(@id, index, identity) check_completion! end def filter_visitor?(request, identity) - @playground.request_filter.call(request) || + @playground.request_filter.call(request) || (@request_filter_block && @request_filter_block.call(request, identity)) end def call_on_assignment_if_available(identity, index) # if we have an on_assignment block, call it on new assignments