You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: cached_expectation.rb
    path of file: ./repos/pakyow/spec/helpers
    
    the code of the file until where you have to start completion: require "fileutils"

module CachedExpectation
  # def expectations_cache_path
  #   raise "not implemented"
  # end

  def cached_expectation(key)
    key_path = expectations_cache_path.join(key
    
  def cached_expectation(key)
    key_path = expectations_cache_path.join(key)
    expected = yield

    if ENV.include?("RECACHE") || !key_path.exist?
      FileUtils.mkdir_p(key_path.dirname)

      key_path.open("w+") { |file|
        file.write(expected)
      }
    else
      expect(key_path.read).to eq(expected)
    end