Class: Scientist::Observation

Inherits:
Object
  • Object
show all
Defined in:
lib/scientist/observation.rb

Overview

What happened when this named behavior was executed? Immutable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, experiment, &block) ⇒ Observation

Returns a new instance of Observation.



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

def initialize(name, experiment, &block)
  @name       = name
  @experiment = experiment
  @now        = Time.now

  begin
    @value = block.call
  rescue Object => e
    @exception = e
  end

  @duration = (Time.now - @now).to_f

  freeze
end

Instance Attribute Details

#durationObject (readonly)

The Float seconds elapsed.



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

def duration
  @duration
end

#exceptionObject (readonly)

The raised exception, if any.



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

def exception
  @exception
end

#experimentObject (readonly)

The experiment this observation is for



5
6
7
# File 'lib/scientist/observation.rb', line 5

def experiment
  @experiment
end

#nameObject (readonly)

The String name of the behavior.



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

def name
  @name
end

#nowObject (readonly)

The instant observation began.



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

def now
  @now
end

#valueObject (readonly)

The value returned, if any.



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

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.



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

def cleaned_value
  if value
    experiment.clean_value value
  end
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
75
76
77
78
79
80
81
82
83
# File 'lib/scientist/observation.rb', line 61

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

  values_are_equal = false
  both_raised      = other.raised? && raised?
  neither_raised   = !other.raised? && !raised?

  if neither_raised
    if block_given?
      values_are_equal = yield value, other.value
    else
      values_are_equal = value == other.value
    end
  end

  exceptions_are_equivalent = # backtraces will differ, natch
    both_raised &&
      other.exception.class == exception.class &&
        other.exception.message == exception.message

  (neither_raised && values_are_equal) ||
    (both_raised && exceptions_are_equivalent)
end

#hashObject



85
86
87
# File 'lib/scientist/observation.rb', line 85

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

#raised?Boolean

Returns:

  • (Boolean)


89
90
91
# File 'lib/scientist/observation.rb', line 89

def raised?
  !exception.nil?
end