Class: AssetRam::Helper

Inherits:
Object
  • Object
show all
Defined in:
lib/asset_ram.rb

Overview

Our own asset helper which memoizes Rails’ asset helper calls.

Constant Summary collapse

@@_cache =
{}

Class Method Summary collapse

Class Method Details

.cache(key: '', &blk) ⇒ Object



45
46
47
48
49
50
# File 'lib/asset_ram.rb', line 45

def self.cache(key: '', &blk)
  cache_key = blk.source_location
  cache_key << key if !key.to_s.empty?

  cache_by_key(cache_key, &blk)
end

.cache_by_key(cache_key, &blk) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/asset_ram.rb', line 53

def self.cache_by_key(cache_key, &blk)
  return yield if ENV['ASSET_RAM_DISABLE']

  if !@@_cache.has_key?(cache_key)
    # Using WARN level because it should only output
    # once during any Rails run. If it's output multiple
    # times, then caching isn't working correctly.
    Rails.logger.warn("Caching #{cache_key}")
    @@_cache[cache_key] = yield
  end

  @@_cache.fetch(cache_key)
end