lib/verdict/experiment.rb in verdict-0.5.0 vs lib/verdict/experiment.rb in verdict-0.6.0

- old
+ new

@@ -91,21 +91,21 @@ def group_handles segmenter.groups.keys end - def subject_assignment(subject_identifier, group, originally_created_at = nil, temporary = false) - Verdict::Assignment.new(self, subject_identifier, group, originally_created_at, temporary) + def subject_assignment(subject, group, originally_created_at = nil, temporary = false) + Verdict::Assignment.new(self, subject, group, originally_created_at, temporary) end - def subject_conversion(subject_identifier, goal, created_at = Time.now.utc) - Verdict::Conversion.new(self, subject_identifier, goal, created_at) + def subject_conversion(subject, goal, created_at = Time.now.utc) + Verdict::Conversion.new(self, subject, goal, created_at) end def convert(subject, goal) identifier = retrieve_subject_identifier(subject) - conversion = subject_conversion(identifier, goal) + conversion = subject_conversion(subject, goal) event_logger.log_conversion(conversion) segmenter.conversion_feedback(identifier, subject, conversion) conversion rescue Verdict::EmptySubjectIdentifier raise unless disqualify_empty_identifier? @@ -119,26 +119,21 @@ assignment_without_unqualified_persistence(identifier, subject, context) end store_assignment(assignment) rescue Verdict::StorageError - subject_assignment(identifier, nil, nil) + nil_assignment(subject) rescue Verdict::EmptySubjectIdentifier if disqualify_empty_identifier? - subject_assignment(identifier, nil, nil) + nil_assignment(subject) else raise end end def assign_manually(subject, group) - identifier = retrieve_subject_identifier(subject) - assign_manually_by_identifier(identifier, group) - end - - def assign_manually_by_identifier(subject_identifier, group) - assignment = subject_assignment(subject_identifier, group) + assignment = subject_assignment(subject, group) if !assignment.qualified? && !store_unqualified? raise Verdict::Error, "Unqualified subject assignments are not stored for this experiment, so manual disqualification is impossible. Consider setting :store_unqualified to true for this experiment." end store_assignment(assignment) @@ -147,40 +142,28 @@ def disqualify_manually(subject) assign_manually(subject, nil) end - def disqualify_manually_by_identifier(subject_identifier) - assign_manually_by_identifier(subject_identifier, nil) - end - def store_assignment(assignment) @storage.store_assignment(assignment) if should_store_assignment?(assignment) event_logger.log_assignment(assignment) assignment end def remove_subject_assignment(subject) - remove_subject_assignment_by_identifier(retrieve_subject_identifier(subject)) + @storage.remove_assignment(self, subject) end - def remove_subject_assignment_by_identifier(subject_identifier) - @storage.remove_assignment(self, subject_identifier) - end - def switch(subject, context = nil) assign(subject, context).to_sym end def lookup(subject) - lookup_assignment_for_identifier(retrieve_subject_identifier(subject)) + @storage.retrieve_assignment(self, subject) end - def lookup_assignment_for_identifier(subject_identifier) - fetch_assignment(subject_identifier) - end - def retrieve_subject_identifier(subject) identifier = subject_identifier(subject).to_s raise Verdict::EmptySubjectIdentifier, "Subject resolved to an empty identifier!" if identifier.empty? identifier end @@ -210,21 +193,22 @@ def to_json(options = {}) as_json(options).to_json end def fetch_subject(subject_identifier) - raise NotImplementedError, "Fetching subjects based in identifier is not implemented for eperiment @{handle.inspect}." + raise NotImplementedError, "Fetching subjects based in identifier is not implemented for experiment @{handle.inspect}." end - def fetch_assignment(subject_identifier) - @storage.retrieve_assignment(self, subject_identifier) - end - def disqualify_empty_identifier? @disqualify_empty_identifier end + def subject_qualifies?(subject, context = nil) + ensure_experiment_has_started + everybody_qualifies? || @qualifier.call(subject, context) + end + protected def default_options {} end @@ -232,46 +216,45 @@ def should_store_assignment?(assignment) assignment.permanent? && !assignment.returning? && (store_unqualified? || assignment.qualified?) end def assignment_with_unqualified_persistence(subject_identifier, subject, context) - previous_assignment = fetch_assignment(subject_identifier) + previous_assignment = lookup(subject) return previous_assignment unless previous_assignment.nil? if subject_qualifies?(subject, context) group = segmenter.assign(subject_identifier, subject, context) - subject_assignment(subject_identifier, group, nil, group.nil?) + subject_assignment(subject, group, nil, group.nil?) else - subject_assignment(subject_identifier, nil, nil) + nil_assignment(subject) end end def assignment_without_unqualified_persistence(subject_identifier, subject, context) if subject_qualifies?(subject, context) - previous_assignment = fetch_assignment(subject_identifier) + previous_assignment = lookup(subject) return previous_assignment unless previous_assignment.nil? group = segmenter.assign(subject_identifier, subject, context) - subject_assignment(subject_identifier, group, nil, group.nil?) + subject_assignment(subject, group, nil, group.nil?) else - subject_assignment(subject_identifier, nil, nil) + nil_assignment(subject) end end def subject_identifier(subject) subject.respond_to?(:id) ? subject.id : subject.to_s end - def subject_qualifies?(subject, context = nil) - ensure_experiment_has_started - everybody_qualifies? || @qualifier.call(subject, context) - end - def set_start_timestamp @storage.store_start_timestamp(self, started_now = Time.now.utc) started_now end def ensure_experiment_has_started @started_at ||= @storage.retrieve_start_timestamp(self) || set_start_timestamp rescue Verdict::StorageError @started_at ||= Time.now.utc + end + + def nil_assignment(subject) + subject_assignment(subject, nil, nil) end end