Class: LabCoat::Observation

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#durationObject (readonly)

Returns the value of attribute duration.



6
7
8
# File 'lib/lab_coat/observation.rb', line 6

def duration
  @duration
end

#errorObject (readonly)

Returns the value of attribute error.



6
7
8
# File 'lib/lab_coat/observation.rb', line 6

def error
  @error
end

#experimentObject (readonly)

Returns the value of attribute experiment.



6
7
8
# File 'lib/lab_coat/observation.rb', line 6

def experiment
  @experiment
end

#nameObject (readonly)

Returns the value of attribute name.



6
7
8
# File 'lib/lab_coat/observation.rb', line 6

def name
  @name
end

#valueObject (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_valueObject



19
20
21
# File 'lib/lab_coat/observation.rb', line 19

def publishable_value
  @publishable_value ||= experiment.publishable_value(self)
end

#raised?TrueClass, FalseClass

Returns:

  • (TrueClass, FalseClass)


24
25
26
# File 'lib/lab_coat/observation.rb', line 24

def raised?
  !error.nil?
end

#slugString

Returns String representing this ‘Observation`.

Returns:

  • (String)

    String representing this ‘Observation`.



29
30
31
# File 'lib/lab_coat/observation.rb', line 29

def slug
  "#{experiment.name}.#{name}"
end

#to_hHash

Returns A hash representation of this ‘Observation`. Useful when publishing `Results`.

Returns:

  • (Hash)

    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&.message
  }.compact
end