1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
# Include this module into any class which requires science experiments in its
# methods. Provides the `science` and `default_scientist_context` methods for
# defining and running experiments.
#
# If you need to run science on class methods, extend this module instead.
#
# If including or extending this module are not an option, call
# `Scientist.run`.
module Scientist
# Define and run a science experiment.
#
# name - a String name for this experiment.
# opts - optional hash with the the named test to run instead of "control",
# :run is the only valid key.
#
# Yields an object which implements the Scientist::Experiment interface.
# See `Scientist::Experiment.new` for how this is defined.
#
# Returns the calculated value of the control experiment, or raises if an
# exception was raised.
def self.run(name, opts = {})
experiment = Experiment.new(name)
yield experiment
test = opts[:run] if opts
experiment.run(test)
end
# Define and run a science experiment.
#
# name - a String name for this experiment.
# opts - optional hash with the the named test to run instead of "control",
# :run is the only valid key.
#
# Yields an object which implements the Scientist::Experiment interface.
# See `Scientist::Experiment.new` for how this is defined. The context from
# the `default_scientist_context` method will be applied to the experiment.
#
# Returns the calculated value of the control experiment, or raises if an
# exception was raised.
def science(name, opts = {})
Scientist.run(name, opts) do |experiment|
experiment.context(default_scientist_context)
yield experiment
end
end
# Public: the default context data for an experiment created and run via the
# `science` helper method. Override this in any class that includes Scientist
# to define your own behavior.
#
# Returns a Hash.
def default_scientist_context
{}
end
end
require "scientist/default"
require "scientist/errors"
require "scientist/experiment"
require "scientist/observation"
require "scientist/result"
require "scientist/version"
|