lib/verdict/experiment.rb in verdict-0.6.3 vs lib/verdict/experiment.rb in verdict-0.7.0
- old
+ new
@@ -1,10 +1,10 @@
class Verdict::Experiment
include Verdict::Metadata
- attr_reader :handle, :qualifier, :storage, :event_logger
+ attr_reader :handle, :qualifiers, :storage, :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
@@ -12,11 +12,11 @@
def initialize(handle, options = {}, &block)
@handle = handle.to_s
options = default_options.merge(options)
- @qualifier = options[:qualifier]
+ @qualifiers = Array(options[:qualifier] || options[:qualifiers])
@event_logger = options[:event_logger] || Verdict::EventLogger.new(Verdict.default_logger)
@storage = storage(options[:storage] || :memory)
@store_unqualified = options[:store_unqualified]
@segmenter = options[:segmenter]
@subject_type = options[:subject_type]
@@ -56,12 +56,20 @@
groups(Verdict::Segmenters::RolloutSegmenter) do
group rollout_group_name, percentage
end
end
- def qualify(&block)
- @qualifier = block
+ def qualify(method_name = nil, &block)
+ if block_given?
+ @qualifiers << block
+ elsif method_name.nil?
+ raise ArgumentError, "no method nor blocked passed!"
+ elsif respond_to?(method_name, true)
+ @qualifiers << method(method_name).to_proc
+ else
+ raise ArgumentError, "No helper for #{method_name.inspect}"
+ end
end
def storage(storage = nil, options = {})
return @storage if storage.nil?
@@ -167,11 +175,11 @@
raise Verdict::EmptySubjectIdentifier, "Subject resolved to an empty identifier!" if identifier.empty?
identifier
end
def has_qualifier?
- !@qualifier.nil?
+ @qualifiers.any?
end
def everybody_qualifies?
!has_qualifier?
end
@@ -202,10 +210,10 @@
@disqualify_empty_identifier
end
def subject_qualifies?(subject, context = nil)
ensure_experiment_has_started
- everybody_qualifies? || @qualifier.call(subject, context)
+ everybody_qualifies? || @qualifiers.all? { |qualifier| qualifier.call(subject, context) }
end
protected
def default_options