module ChefSpec::Cacher
def cached(name, &block)
def cached(name, &block) location = ancestors.first.metadata[:location] unless location.nil? location += ancestors.first.metadata[:description] unless ancestors.first.metadata[:description].nil? location += ancestors.first.metadata[:scoped_id] unless ancestors.first.metadata[:scoped_id].nil? end location ||= ancestors.first.metadata[:parent_example_group][:location] define_method(name) do key = [location, name.to_s].join(".") unless @@cache.key?(Thread.current.object_id) ObjectSpace.define_finalizer(Thread.current, FINALIZER) end @@cache[Thread.current.object_id] ||= {} @@cache[Thread.current.object_id][key] ||= instance_eval(&block) end end
def cached!(name, &block)
def cached!(name, &block) cached(name, &block) before { send(name) } end