module Rabl
Defines the default cache engine for RABL when caching is invoked for a template. You can define your own caching engines by creating an object that responds to fetch and setting the configuration option:
config.cache_engine = AdvancedCacheEngine.new
Constants
- CACHE_KEY_VERSION
Marks every cache key rabl reads or writes. Bump it when a release changes rendered output so that entries cached by earlier versions would be wrong: the keys change once, so no entry cached by an earlier version, which lacks that release's output fixes, is served. Keys before 0.18.0 had no marker (“rabl/”, and “rabl_build/” in old releases); 2 is 0.18.0. The README's caching example shows the resulting prefix: update it when this changes.
- RENDER_SOURCE_CACHE
Fiber-local key of the sources read by the render in progress (see
Rabl.begin_render_source_cache)- VERSION
Public Class Methods
Internal: Starts collecting the sources the render about to run reads (see source_cache). The render that starts a collection ends it, so a render started inside another collects its own sources for as long as it runs and the render around it reads again afterwards.
# File lib/rabl.rb, line 93 def begin_render_source_cache Thread.current[RENDER_SOURCE_CACHE] = {} end
Returns the configuration options set for RABL Rabl.configuration.include_json_root => false
# File lib/rabl.rb, line 55 def configuration @_configuration ||= Configuration.new end
Yields a RABL configuration block Rabl.configure do |config|
config.include_json_root = false config.enable_json_callbacks = true
end
# File lib/rabl.rb, line 48 def configure(&block) yield(configuration) configuration end
Internal: Drops the collection the render that returns from it started.
# File lib/rabl.rb, line 98 def end_render_source_cache Thread.current[RENDER_SOURCE_CACHE] = nil end
Returns a copy of value whose Strings and Arrays are frozen, for cache keys that must not change when the caller's objects do
# File lib/rabl.rb, line 104 def frozen_copy(value) case value when Array then value.map { |item| frozen_copy(item) }.freeze when String then value.frozen? ? value : value.dup.freeze else value end end
Initialize RABL within an application Rabl.register!
# File lib/rabl.rb, line 39 def register! require 'rabl/template' end
Renders an object using a specified template within an application. render(@post, 'posts/show', :view_path => “/path/to/app/views”)
# File lib/rabl.rb, line 114 def render(object, source, options = {}) Rabl::Renderer.new(source, object, options).render end
The template sources read so far by the render running in this fiber, if any
# File lib/rabl.rb, line 86 def render_source_cache Thread.current[RENDER_SOURCE_CACHE] end
Resets the RABL configuration back to the defaults.
# File lib/rabl.rb, line 60 def reset_configuration! @_configuration = nil end
Resets the RABL source cache
# File lib/rabl.rb, line 81 def reset_source_cache! @_source_cache = {} end
Fetches from the source_cache, stores block result in cache if nil Used to cache the contents and paths to various rabl templates, keyed by everything the lookup depends on (the file, the view path and the lookup details) source_cache(“users/index”, [“path/to/view”], [:json]) { “/full/path/to/template/users/index” }
# File lib/rabl.rb, line 68 def source_cache(file, view_path, lookup = nil) # With cache_sources off, a template the collection uses for each item (extends, partial) is # still read and resolved once per render rather than once per item. That cache belongs to # the render, so a template changed between renders is still read again. cache = Rabl.configuration.cache_sources ? (@_source_cache ||= {}) : render_source_cache return yield unless cache # stores a frozen copy of the key, so later changes to the caller's strings don't reach it key = [file, view_path, lookup] cache.fetch(key) { cache[frozen_copy(key)] = yield } end