Skip to content

Commit b679228

Browse files
committed
Add experimental callback for mucking about with results after recording
1 parent d132645 commit b679228

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

app/models/lab_tech/experiment.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ def self.named(experiment_name_or_id)
5656

5757
##### INSTANCE METHODS #####
5858

59+
# This method is experimental and should probably not be depended on
60+
def __after_recording__(&block)
61+
@after_recording = block
62+
end
63+
5964
def comparator
6065
@_scientist_comparator
6166
end
@@ -96,7 +101,10 @@ def name=(value) ; write_attribute :name, value ; end
96101

97102
def publish(scientist_result)
98103
return if Rails.env.test? && !LabTech.publish_results_in_test_mode?
99-
LabTech::Result.record_a_science( self, scientist_result, diff_with: @diff_with )
104+
105+
LabTech::Result.record_a_science( self, scientist_result, diff_with: @diff_with ).tap do |result|
106+
@after_recording&.call result
107+
end
100108
end
101109

102110
# I don't encourage the willy-nilly destruction of experimental results...

spec/models/lab_tech/experiment_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,21 @@ def wtf
9292
end
9393
end
9494

95+
specify "clients can intercept the LabTech::Result before it gets recorded (EXPERIMENTAL)" do
96+
LabTech.science "wibble" do |e|
97+
e.context( { a: 1 } )
98+
e.use { :hello_world }
99+
e.try { :howdy_earth }
100+
101+
e.__after_recording__ do |result|
102+
result.update context: { b: 2 }
103+
end
104+
end
105+
106+
result = experiment.results.first
107+
expect( result.context ).to eq( { b: 2 } )
108+
end
109+
95110
describe "diff-generating behavior" do
96111
let(:result) { experiment.results.first }
97112

0 commit comments

Comments
 (0)