lib/dat/science/experiment.rb in dat-science-1.1.0 vs lib/dat/science/experiment.rb in dat-science-1.2.0

- old
+ new

@@ -11,10 +11,12 @@ # Public: Create a new experiment instance. `self` is yielded to an # optional `block` if it's provided. def initialize(name, &block) @candidate = nil + @cleaner = lambda { |r| r } + @comparator = lambda { |a, b| a == b } @context = { :experiment => name } @control = nil @name = name yield self if block_given? @@ -25,10 +27,42 @@ def candidate(&block) @candidate = block if block @candidate end + # Internal: Run the cleaner on a value. + def clean(value) + cleaner.call value + end + + # Public: Declare a cleaner `block` to scrub the result before it's + # published. `block` is called twice, once with the result of + # the control behavior and once with the result of the candidate. + # Exceptions during cleaning are treated as if they were raised + # in a candidate or control behavior block: They're reported as part + # of the result. + # + # Returns `block`. + def cleaner(&block) + @cleaner = block if block + @cleaner + end + + # Internal: Run the comparator on two values. + def compare(a, b) + comparator.call a, b + end + + # Public: Declare a comparator `block`. Results are compared with + # `==` by default. + # + # Returns `block`. + def comparator(&block) + @comparator = block if block + @comparator + end + # Public: Add a Hash of `payload` data to be included when events are # published or returns the current context if `payload` is `nil`. def context(payload = nil) @context.merge! payload if payload @context @@ -105,10 +139,10 @@ rescue => ex raised = ex end duration = (Time.now - start) * 1000 - Science::Result.new value, duration, raised + Science::Result.new self, value, duration, raised end # Internal. Returns a Dat::Science::Result for `candidate`. def observe_candidate