class Rabl::MultiBuilder
Public Class Methods
new(data, settings = {}, options = {})
click to toggle source
Constructs a new MultiBuilder given the data and options. The options will be re-used for all Rabl::Builders. Rabl::MultiBuilder.new([#<User …>, #<User …>, …], { :format => 'json', :child_root => true })
# File lib/rabl/multi_builder.rb, line 10 def initialize(data, settings = {}, options = {}) @data = data @settings = settings @options = options @builders = [] @engine_to_builder = {} @cache_key_to_engine = {} end
Public Instance Methods
to_a()
click to toggle source
Returns the result of all of the builders as an array
# File lib/rabl/multi_builder.rb, line 20 def to_a generate_builders if template_cache_configured? && Rabl.configuration.use_read_multi map_engines_to_builders read_cache_results replace_engines_with_cache_results end result = @builders.map(&:to_hash) result = result.map(&:presence).compact if Rabl.configuration.exclude_empty_values_in_collections result end
Private Instance Methods
cache_key_for(engine)
click to toggle source
# File lib/rabl/multi_builder.rb, line 73 def cache_key_for(engine) case engine when Hash cache_key_for(engine.values.first) when Engine engine.cache_key end end
disable_cache_read_on_render(engine)
click to toggle source
# File lib/rabl/multi_builder.rb, line 64 def disable_cache_read_on_render(engine) case engine when Hash disable_cache_read_on_render(engine.values.first) when Engine engine.cache_read_on_render = false end end
generate_builders()
click to toggle source
Creates a builder for each of the data objects and maps the cache keys for each of the engines the builders generated
# File lib/rabl/multi_builder.rb, line 39 def generate_builders @builders = @data.map do |object| Builder.new(object, @settings, @options) end end
map_cache_key_to_engine(engine)
click to toggle source
Maps a cache key to an engine
# File lib/rabl/multi_builder.rb, line 56 def map_cache_key_to_engine(engine) if cache_key = cache_key_for(engine) result_cache_key = TemplateCacheKey.expand(cache_key) @cache_key_to_engine[result_cache_key] = engine disable_cache_read_on_render(engine) end end
map_engines_to_builders()
click to toggle source
# File lib/rabl/multi_builder.rb, line 45 def map_engines_to_builders @builders.each do |builder| builder.engines.each do |engine| @engine_to_builder[engine] = builder map_cache_key_to_engine(engine) end end end
read_cache_results()
click to toggle source
Returns the items that were found in the cache
# File lib/rabl/multi_builder.rb, line 83 def read_cache_results @cache_results ||= begin mutable_keys = @cache_key_to_engine.keys.map { |k| k.dup } if mutable_keys.empty? {} else Rabl.configuration.cache_engine.read_multi(*mutable_keys) end end end
replace_engines_with_cache_results()
click to toggle source
Maps the results from the cache back to the builders
# File lib/rabl/multi_builder.rb, line 95 def replace_engines_with_cache_results @cache_results.each do |key, value| engine = @cache_key_to_engine[key] builder = @engine_to_builder[engine] next unless value && engine && builder # a child is stored as { name => #<Engine> }; the cache holds only its rendered body value = { engine.keys.first => value } if engine.is_a?(Hash) builder.replace_engine(engine, value) end end