lib/split/experiment.rb in split-0.2.2 vs lib/split/experiment.rb in split-0.2.3
- old
+ new
@@ -1,14 +1,16 @@
module Split
class Experiment
attr_accessor :name
attr_accessor :alternative_names
attr_accessor :winner
+ attr_accessor :version
def initialize(name, *alternative_names)
@name = name.to_s
@alternative_names = alternative_names
+ @version = (Split.redis.get("#{name.to_s}:version").to_i || 0)
end
def winner
if w = Split.redis.hget(:experiment_winner, name)
return Split::Alternative.find(w, name)
@@ -35,14 +37,40 @@
def next_alternative
winner || alternatives.sort_by{|a| a.participant_count + rand}.first
end
+ def version
+ @version ||= 0
+ end
+
+ def increment_version
+ @version += 1
+ Split.redis.set("#{name}:version", @version)
+ end
+
+ def key
+ if version.to_i > 0
+ "#{name}:#{version}"
+ else
+ name
+ end
+ end
+
def reset
alternatives.each(&:reset)
reset_winner
+ increment_version
end
+
+ def delete
+ alternatives.each(&:delete)
+ reset_winner
+ Split.redis.srem(:experiments, name)
+ Split.redis.del(name)
+ increment_version
+ end
def new_record?
!Split.redis.exists(name)
end
@@ -75,10 +103,11 @@
else
raise 'Experiment not found'
end
end
- def self.find_or_create(name, *alternatives)
+ def self.find_or_create(key, *alternatives)
+ name = key.split(':')[0]
if Split.redis.exists(name)
if load_alternatives_for(name) == alternatives
experiment = self.new(name, *load_alternatives_for(name))
else
exp = self.new(name, *load_alternatives_for(name))
\ No newline at end of file