module Rabl::TemplateCacheKey

Builds the parts of template cache keys. These are module functions rather than Engine methods so that they never shadow helpers a template calls.

Public Instance Methods

column(block) click to toggle source

Returns the column a block starts at, which tells apart blocks on one line, or nil when the runtime doesn't expose it. ceiling: without RubyVM::InstructionSequence (e.g. on JRuby), blocks that start on the same line of a template share an identity; put them on separate lines, or give them distinct cache keys.

# File lib/rabl/template_cache_key.rb, line 51
def column(block)
  return unless defined?(RubyVM::InstructionSequence)

  iseq = RubyVM::InstructionSequence.of(block)
  iseq && iseq.to_a[4][:code_location][1]
rescue StandardError
  nil
end
digest(parts) click to toggle source

Returns a digest of the given parts TemplateCacheKey.digest([“app/views/show.rabl”, “object @user”]) => “4a1b…”

# File lib/rabl/template_cache_key.rb, line 27
def digest(parts)
  digest = Digest::SHA1.new
  Array(parts).flatten.each { |part| digest << part.to_s << "\0" }
  digest.hexdigest
end
expand(parts) click to toggle source

Returns the key rabl reads and writes in the cache engine for the given parts. Earlier versions put their keys under “rabl/”, so no key built here can be one of theirs. TemplateCacheKey.expand([user, “json”]) => “rabl-v2/users/1/json”

# File lib/rabl/template_cache_key.rb, line 21
def expand(parts)
  ActiveSupport::Cache.expand_cache_key(parts, "rabl-v#{CACHE_KEY_VERSION}")
end
path(path, root = self.root) click to toggle source

Returns a path that stays the same when the application moves: relative to the working directory when inside it, and nil for “(eval)” locations (the identity of the parent template tells evaluated sources apart). ceiling: paths outside the working directory stay absolute, so moving them changes the keys; a configurable root would let those move too. TemplateCacheKey.path(“/app/views/show.rabl”, “/app/”) => “views/show.rabl”

# File lib/rabl/template_cache_key.rb, line 39
def path(path, root = self.root)
  path = path.to_s.dup.force_encoding(Encoding::BINARY) # paths may differ in encoding
  return if path.empty? || path.start_with?("(eval")

  root && path.start_with?(root) ? path.byteslice(root.bytesize..-1) : path
end
root() click to toggle source

Returns the working directory with a trailing separator, as bytes, or nil if it is gone

# File lib/rabl/template_cache_key.rb, line 61
def root
  File.join(Dir.pwd, "").force_encoding(Encoding::BINARY)
rescue SystemCallError
  nil
end
shape(options, lookup_format) click to toggle source

Returns the options that change the shape of a rendering. A nested (hash format) rendering gets its root options from its parent; for other formats only root options given explicitly are added, so default keys have none. The format `extends` and `partial` look templates up in (lookup_format) is added when it isn't the one rendered, a hash counting as JSON: a nested rendering is a hash for every format. TemplateCacheKey.shape({ :format => “hash”, :root => true }, “json”) => [“root”, “no_child_root”] TemplateCacheKey.shape({ :format => “hash”, :child_root => true }, “xml”) => [“lookup_format=xml”] TemplateCacheKey.shape({ :format => “json”, :except => :b }, “json”) => [“except=:b”]

# File lib/rabl/template_cache_key.rb, line 75
def shape(options, lookup_format)
  shape = []
  format = options[:format].to_s

  if format == "hash"
    shape << "root" if options[:root]
    shape << "no_child_root" unless options[:child_root]
    shape << "object_root=#{options[:object_root_name].inspect}" if options[:object_root_name]
  else
    shape << "root=#{options[:root].inspect}" if options.key?(:root)
    shape << "child_root=#{options[:child_root].inspect}" if options.key?(:child_root)
  end

  shape << "except=#{Array(options[:except]).map { |name| name.inspect }.join(',')}" if options[:except]
  shape << "lookup_format=#{lookup_format}" unless lookup_format == (format == "hash" ? "json" : format)
  shape
end