Class: Scientist::Observation

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

Instance Method Summary collapse

Constructor Details

#initialize(name, experiment, fabricated_duration: nil, &block) ⇒ Observation

Returns a new instance of Observation.


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/scientist/observation.rb', line 23

def initialize(name, experiment, fabricated_duration: nil, &block)
  @name       = name
  @experiment = experiment

  starting = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second) unless fabricated_duration
  begin
    @value = block.call
  rescue *RESCUES => e
    @exception = e
  end

  @duration = fabricated_duration ||
    Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_second) - starting

  freeze
end

Instance Attribute Details

#durationObject (readonly)

The Float seconds elapsed.


21
22
23
# File 'lib/scientist/observation.rb', line 21

def duration
  @duration
end

#exceptionObject (readonly)

The raised exception, if any.


18
19
20
# File 'lib/scientist/observation.rb', line 18

def exception
  @exception
end

#experimentObject (readonly)

The experiment this observation is for


9
10
11
# File 'lib/scientist/observation.rb', line 9

def experiment
  @experiment
end

#nameObject (readonly)

The String name of the behavior.


12
13
14
# File 'lib/scientist/observation.rb', line 12

def name
  @name
end

#valueObject (readonly)

The value returned, if any.


15
16
17
# File 'lib/scientist/observation.rb', line 15

def value
  @value
end

Instance Method Details

#cleaned_valueObject

Return a cleaned value suitable for publishing. Uses the experiment's defined cleaner block to clean the observed value.


42
43
44
# File 'lib/scientist/observation.rb', line 42

def cleaned_value
  experiment.clean_value value unless value.nil?
end

#equivalent_to?(other, &comparator) ⇒ Boolean

Is this observation equivalent to another?

other - the other Observation in question comparator - an optional comparison block. This observation's value and the

other observation's value are yielded to this to determine
their equivalency. Block 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 block, if given

  • Both observations raised an exception with the same class and message.

Returns false otherwise.

Returns:

  • (Boolean)

61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/scientist/observation.rb', line 61

def equivalent_to?(other, &comparator)
  return false unless other.is_a?(Scientist::Observation)

  if raised? || other.raised?
    return other.exception.class == exception.class &&
      other.exception.message == exception.message
  end

  if comparator
    comparator.call(value, other.value)
  else
    value == other.value
  end
end

#hashObject


76
77
78
# File 'lib/scientist/observation.rb', line 76

def hash
  [value, exception, self.class].compact.map(&:hash).inject(:^)
end

#raised?Boolean

Returns:

  • (Boolean)

80
81
82
# File 'lib/scientist/observation.rb', line 80

def raised?
  !exception.nil?
end