Class: LabCoat::Observation
- Inherits:
-
Object
- Object
- LabCoat::Observation
- Defined in:
- lib/lab_coat/observation.rb
Overview
A wrapper around some behavior that captures the resulting ‘value` and any exceptions thrown.
Instance Attribute Summary collapse
-
#duration ⇒ Object
readonly
Returns the value of attribute duration.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#experiment ⇒ Object
readonly
Returns the value of attribute experiment.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
-
#initialize(name, experiment, &block) ⇒ Observation
constructor
A new instance of Observation.
- #publishable_value ⇒ Object
- #raised? ⇒ TrueClass, FalseClass
-
#slug ⇒ String
String representing this ‘Observation`.
-
#to_h ⇒ Hash
A hash representation of this ‘Observation`.
Constructor Details
#initialize(name, experiment, &block) ⇒ Observation
Returns a new instance of Observation.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/lab_coat/observation.rb', line 8 def initialize(name, experiment, &block) @name = name @experiment = experiment @duration = Benchmark.measure(name) do @value = block.call rescue StandardError => e @error = e end end |
Instance Attribute Details
#duration ⇒ Object (readonly)
Returns the value of attribute duration.
6 7 8 |
# File 'lib/lab_coat/observation.rb', line 6 def duration @duration end |
#error ⇒ Object (readonly)
Returns the value of attribute error.
6 7 8 |
# File 'lib/lab_coat/observation.rb', line 6 def error @error end |
#experiment ⇒ Object (readonly)
Returns the value of attribute experiment.
6 7 8 |
# File 'lib/lab_coat/observation.rb', line 6 def experiment @experiment end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/lab_coat/observation.rb', line 6 def name @name end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
6 7 8 |
# File 'lib/lab_coat/observation.rb', line 6 def value @value end |
Instance Method Details
#publishable_value ⇒ Object
19 20 21 |
# File 'lib/lab_coat/observation.rb', line 19 def publishable_value @publishable_value ||= experiment.publishable_value(self) end |
#raised? ⇒ TrueClass, FalseClass
24 25 26 |
# File 'lib/lab_coat/observation.rb', line 24 def raised? !error.nil? end |
#slug ⇒ String
Returns String representing this ‘Observation`.
29 30 31 |
# File 'lib/lab_coat/observation.rb', line 29 def slug "#{experiment.name}.#{name}" end |
#to_h ⇒ Hash
Returns A hash representation of this ‘Observation`. Useful when publishing `Results`.
34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/lab_coat/observation.rb', line 34 def to_h { name: name, experiment: experiment.name, slug: slug, value: publishable_value, duration: duration.to_h, error_class: error&.class&.name, error_message: error&. }.compact end |