Browse files

Inline memoize helper

  • Loading branch information...
1 parent a1f81af commit f556acabb17832ff94bdfbd58362a670ce6a40d1 @josh josh committed Oct 10, 2011
Showing with 15 additions and 11 deletions.
  1. +15 −11 lib/sprockets/index.rb
View
26 lib/sprockets/index.rb
@@ -38,7 +38,12 @@ def index
# Cache calls to `file_digest`
def file_digest(pathname)
- memoize(@digests, pathname.to_s) { super }
+ key = pathname.to_s
+ if @digests.key?(key)
+ @digests[key]
+ else
+ @digests[key] = super
+ end
end
# Cache `find_asset` calls
@@ -63,18 +68,17 @@ def expire_index!
# Cache asset building in memory and in persisted cache.
def build_asset(path, pathname, options)
# Memory cache
- memoize(@assets, cache_key_for(pathname, options)) do
- # Persisted cache
- cache_asset(cache_key_for(pathname, options)) do
- super
+ key = cache_key_for(pathname, options)
+ if @assets.key?(key)
+ @assets[key]
+ else
+ @assets[key] = begin
+ # Persisted cache
+ cache_asset(key) do
+ super
+ end
end
end
end
-
- private
- # Simple memoize helper that stores `nil` values
- def memoize(hash, key)
- hash.key?(key) ? hash[key] : hash[key] = yield
- end
end
end

0 comments on commit f556aca

Please sign in to comment.