lib/metry/experiment.rb in metry-1.2.0 vs lib/metry/experiment.rb in metry-2.0.0

- old
+ new

@@ -1,23 +1,21 @@ module Metry class Experiment METHODS = { "rand" => proc{|list, visitor| list.sort_by{rand}.first}, - "mod_visitor" => proc{|list, visitor| list[(visitor.to_i-1)%list.size]}, + "mod_visitor" => proc{|list, visitor| list[(visitor['_id'].to_i-1)%list.size]}, # mod_visitor will only work with predictable_keys = true } - def initialize(name, event) - @key = "experiment.#{name}" + def initialize(name, event, visitor) + @key = "experiment:#{name}" @event = event - @visitor = event["visitor"] - @storage = Metry.current + @visitor = visitor end def choose(options, method=nil) - visitor_hash = @storage.visitor(@visitor) - unless choice = visitor_hash[@key] + unless choice = @visitor[@key] choice = METHODS[method || "rand"][options.keys, @visitor] - @storage.save_visitor(@visitor, visitor_hash.merge(@key => choice)) + @visitor[@key] = choice end @event[@key] = choice options[choice] end end \ No newline at end of file