lib/verdict/experiment.rb in verdict-0.2.0 vs lib/verdict/experiment.rb in verdict-0.2.1
- old
+ new
@@ -14,19 +14,20 @@
@handle = handle.to_s
options = default_options.merge(options)
@qualifier = options[:qualifier]
@event_logger = options[:event_logger] || Verdict::EventLogger.new(Verdict.default_logger)
- @subject_storage = options[:storage] || Verdict::Storage::MemoryStorage.new
+ @subject_storage = storage(options[:storage] || :memory)
@store_unqualified = options[:store_unqualified]
@segmenter = options[:segmenter]
@subject_type = options[:subject_type]
@disqualify_empty_identifier = options[:disqualify_empty_identifier]
instance_eval(&block) if block_given?
end
+
def subject_type(type = nil)
return @subject_type if type.nil?
@subject_type = type
end
@@ -36,31 +37,38 @@
def group(handle)
segmenter.groups[handle.to_s]
end
- def groups(segmenter_class = Verdict::FixedPercentageSegmenter, &block)
+ def groups(segmenter_class = Verdict::Segmenters::FixedPercentageSegmenter, &block)
return segmenter.groups unless block_given?
@segmenter ||= segmenter_class.new(self)
@segmenter.instance_eval(&block)
@segmenter.verify!
return self
end
def rollout_percentage(percentage, rollout_group_name = :enabled)
- groups(Verdict::RolloutSegmenter) do
+ groups(Verdict::Segmenters::RolloutSegmenter) do
group rollout_group_name, percentage
end
end
def qualify(&block)
@qualifier = block
end
- def storage(subject_storage, options = {})
+ def storage(subject_storage = nil, options = {})
+ return @subject_storage if subject_storage.nil?
+
@store_unqualified = options[:store_unqualified] if options.has_key?(:store_unqualified)
- @subject_storage = subject_storage
+ @subject_storage = case subject_storage
+ when :memory; Verdict::Storage::MemoryStorage.new
+ when :none; Verdict::Storage::MockStorage.new
+ when Class; subject_storage.new
+ else subject_storage
+ end
end
def segmenter
raise Verdict::Error, "No groups defined for experiment #{@handle.inspect}." if @segmenter.nil?
@segmenter
@@ -182,11 +190,11 @@
end
end
def to_json(options = {})
as_json(options).to_json
- end
+ end
def fetch_subject(subject_identifier)
raise NotImplementedError, "Fetching subjects based in identifier is not implemented for eperiment @{handle.inspect}."
end
@@ -219,10 +227,10 @@
if subject_qualifies?(subject, context)
previous_assignment = fetch_assignment(subject_identifier)
return previous_assignment unless previous_assignment.nil?
group = segmenter.assign(subject_identifier, subject, context)
subject_assignment(subject_identifier, group, nil, group.nil?)
- else
+ else
subject_assignment(subject_identifier, nil, nil)
end
end
def subject_identifier(subject)