lib/eeny-meeny/models/experiment.rb in eeny-meeny-2.2.2 vs lib/eeny-meeny/models/experiment.rb in eeny-meeny-2.3.0

- old
+ new

@@ -1,13 +1,16 @@ +require 'eeny-meeny/models/cookie' require 'eeny-meeny/models/variation' require 'active_support/time' require 'active_support/core_ext/enumerable' module EenyMeeny class Experiment - COOKIE_EXPERIMENT_ID_REGEX = /\Aeeny_meeny_(.+)_v\d+\z/.freeze + COOKIE_EXPERIMENT_ID_REGEX = Regexp.new( + "\\A#{EenyMeeny::Cookie::EXPERIMENT_PREFIX}(.+)_v(\\d+)\\z" + ).freeze attr_reader :id, :name, :version, :variations, :total_weight, :end_at, :start_at def self.find_all return [] unless EenyMeeny.config.experiments @@ -21,12 +24,15 @@ experiment = EenyMeeny.config.experiments[experiment_id.to_sym] new(experiment_id, **experiment) if experiment end def self.find_by_cookie_name(cookie_name) - if cookie_name =~ COOKIE_EXPERIMENT_ID_REGEX - find_by_id($1) - end + return unless cookie_name =~ COOKIE_EXPERIMENT_ID_REGEX + + experiment = find_by_id($1) + return unless experiment && experiment.version.to_s == $2 + + experiment end def initialize(id, name: '', version: 1, variations: {}, start_at: nil, end_at: nil) @id = id @name = name