Class: Scientist::Observation
- Inherits:
-
Object
- Object
- Scientist::Observation
- Defined in:
- lib/scientist/observation.rb
Overview
What happened when this named behavior was executed? Immutable.
Constant Summary collapse
- RESCUES =
An Array of Exception types to rescue when initializing an observation. NOTE: This Array will change to ‘[StandardError]` in the next major release.
[Exception]
Instance Attribute Summary collapse
-
#cpu_time ⇒ Object
readonly
The Float CPU time elapsed, in seconds.
-
#duration ⇒ Object
readonly
The Float seconds elapsed.
-
#exception ⇒ Object
readonly
The raised exception, if any.
-
#experiment ⇒ Object
readonly
The experiment this observation is for.
-
#name ⇒ Object
readonly
The String name of the behavior.
-
#value ⇒ Object
readonly
The value returned, if any.
Instance Method Summary collapse
-
#cleaned_value ⇒ Object
Return a cleaned value suitable for publishing.
-
#equivalent_to?(other, comparator = nil, error_comparator = nil) ⇒ Boolean
Is this observation equivalent to another?.
- #hash ⇒ Object
-
#initialize(name, experiment, fabricated_duration: nil, &block) ⇒ Observation
constructor
A new instance of Observation.
- #raised? ⇒ Boolean
Constructor Details
#initialize(name, experiment, fabricated_duration: nil, &block) ⇒ Observation
Returns a new instance of Observation.
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 |
# File 'lib/scientist/observation.rb', line 26 def initialize(name, experiment, fabricated_duration: nil, &block) @name = name @experiment = experiment start_wall_time, start_cpu_time = capture_times unless fabricated_duration begin @value = block.call rescue *RESCUES => e @exception = e end if fabricated_duration.is_a?(Hash) @duration = fabricated_duration["duration"] @cpu_time = fabricated_duration["cpu_time"] elsif fabricated_duration @duration = fabricated_duration @cpu_time = 0.0 # setting a default value else end_wall_time, end_cpu_time = capture_times @duration = end_wall_time - start_wall_time @cpu_time = end_cpu_time - start_cpu_time end freeze end |
Instance Attribute Details
#cpu_time ⇒ Object (readonly)
The Float CPU time elapsed, in seconds
24 25 26 |
# File 'lib/scientist/observation.rb', line 24 def cpu_time @cpu_time end |
#duration ⇒ Object (readonly)
The Float seconds elapsed.
21 22 23 |
# File 'lib/scientist/observation.rb', line 21 def duration @duration end |
#exception ⇒ Object (readonly)
The raised exception, if any.
18 19 20 |
# File 'lib/scientist/observation.rb', line 18 def exception @exception end |
#experiment ⇒ Object (readonly)
The experiment this observation is for
9 10 11 |
# File 'lib/scientist/observation.rb', line 9 def experiment @experiment end |
#name ⇒ Object (readonly)
The String name of the behavior.
12 13 14 |
# File 'lib/scientist/observation.rb', line 12 def name @name end |
#value ⇒ Object (readonly)
The value returned, if any.
15 16 17 |
# File 'lib/scientist/observation.rb', line 15 def value @value end |
Instance Method Details
#cleaned_value ⇒ Object
Return a cleaned value suitable for publishing. Uses the experiment’s defined cleaner block to clean the observed value.
55 56 57 |
# File 'lib/scientist/observation.rb', line 55 def cleaned_value experiment.clean_value value unless value.nil? end |
#equivalent_to?(other, comparator = nil, error_comparator = nil) ⇒ Boolean
Is this observation equivalent to another?
other - the other Observation in question comparator - an optional comparison proc. This observation’s value and the
other observation's value are passed to this to determine
their equivalency. Proc should return true/false.
error_comparator - an optional comparison proc. This observation’s Error and the
other observation's Error are passed to this to determine
their equivalency. Proc should return true/false.
Returns true if:
-
The values of the observation are equal (using ‘==`)
-
The values of the observations are equal according to a comparison proc, if given
-
The exceptions raised by the observations are equal according to the error comparison proc, if given.
-
Both observations raised an exception with the same class and message.
Returns false otherwise.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/scientist/observation.rb', line 79 def equivalent_to?(other, comparator=nil, error_comparator=nil) return false unless other.is_a?(Scientist::Observation) if raised? || other.raised? if error_comparator return error_comparator.call(exception, other.exception) else return other.exception.class == exception.class && other.exception. == exception. end end if comparator comparator.call(value, other.value) else value == other.value end end |
#hash ⇒ Object
98 99 100 |
# File 'lib/scientist/observation.rb', line 98 def hash [value, exception, self.class].compact.map(&:hash).inject(:^) end |
#raised? ⇒ Boolean
102 103 104 |
# File 'lib/scientist/observation.rb', line 102 def raised? !exception.nil? end |