How to clean up yaml data during or before build?

I have some data in a yaml file. I want to calculate some values from this data, which I’ll then use in pages built from a template. So far I’ve been doing this sort of calculation in helpers but it involves a lot of repetition (e.g. the same calculation is made multiple times for one page) and is really a mis-use of helpers.

I’m looking for an alternative, better way of doing this. What I’d like to do is at build time process the data file and do the calculations once so that the results can just be read (as many times as necessary) when I build the pages.

In my config.rb file I envisage something like:

#call some code here to process data.products here, either overwriting the products.yml or creating a new file such as products_cleaned.yml

#this code already exists...
data.products.each do |p|
    proxy product.url, "/product.html", locals: { product: p}, ignore: true
end

What’s the best way to do this? Do I need to write an extension, or is there another way I can call some ruby to munge my yaml file? Any help would be appreciated.