lib/verdict/experiment.rb in verdict-0.11.0 vs lib/verdict/experiment.rb in verdict-0.12.0
- old
+ new
@@ -1,18 +1,19 @@
class Verdict::Experiment
include Verdict::Metadata
- attr_reader :handle, :qualifiers, :storage, :event_logger
+ attr_reader :handle, :qualifiers, :event_logger
def self.define(handle, *args, &block)
experiment = self.new(handle, *args, &block)
raise Verdict::ExperimentHandleNotUnique.new(experiment.handle) if Verdict.repository.has_key?(experiment.handle)
Verdict.repository[experiment.handle] = experiment
end
def initialize(handle, options = {}, &block)
+ @started_at = nil
@handle = handle.to_s
options = default_options.merge(options)
@qualifiers = Array(options[:qualifier] || options[:qualifiers])
@event_logger = options[:event_logger] || Verdict::EventLogger.new(Verdict.default_logger)
@@ -86,11 +87,11 @@
@segmenter
end
def started_at
@started_at ||= @storage.retrieve_start_timestamp(self)
- rescue Verdict::StorageError => e
+ rescue Verdict::StorageError
nil
end
def started?
!@started_at.nil?
@@ -160,12 +161,12 @@
@storage.store_assignment(assignment) if should_store_assignment?(assignment)
event_logger.log_assignment(assignment)
assignment
end
- def cleanup
- @storage.cleanup(handle.to_s)
+ def cleanup(options = {})
+ @storage.cleanup(self, options)
end
def remove_subject_assignment(subject)
@storage.remove_assignment(self, subject)
end
@@ -191,18 +192,16 @@
def everybody_qualifies?
!has_qualifier?
end
def as_json(options = {})
- data = {
+ {
handle: handle,
has_qualifier: has_qualifier?,
- groups: segmenter.groups.values.map { |g| g.as_json(options) },
+ groups: segmenter.groups.values.map { |group| group.as_json(options) },
metadata: metadata,
started_at: started_at.nil? ? nil : started_at.utc.strftime('%FT%TZ')
- }
-
- data.tap do |data|
+ }.tap do |data|
data[:subject_type] = subject_type.to_s unless subject_type.nil?
end
end
def to_json(options = {})