1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/actionview/lib/action_view/digestor.rb

129 lines
4.1 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "action_view/dependency_tracker"
module ActionView
class Digestor
@@digest_mutex = Mutex.new
class << self
# Supported options:
#
# * <tt>name</tt> - Template name
# * <tt>format</tt> - Template format
# * <tt>finder</tt> - An instance of <tt>ActionView::LookupContext</tt>
# * <tt>dependencies</tt> - An array of dependent views
def digest(name:, format: nil, finder:, dependencies: nil)
if dependencies.nil? || dependencies.empty?
cache_key = "#{name}.#{format}"
else
dependencies_suffix = dependencies.flatten.tap(&:compact!).join(".")
cache_key = "#{name}.#{format}.#{dependencies_suffix}"
end
# this is a correctly done double-checked locking idiom
# (Concurrent::Map's lookups have volatile semantics)
finder.digest_cache[cache_key] || @@digest_mutex.synchronize do
finder.digest_cache.fetch(cache_key) do # re-check under lock
partial = name.include?("/_")
root = tree(name, finder, partial)
dependencies.each do |injected_dep|
root.children << Injected.new(injected_dep, nil, nil)
end if dependencies
finder.digest_cache[cache_key] = root.digest(finder)
end
end
end
def logger
ActionView::Base.logger || NullLogger
end
# Create a dependency tree for template named +name+.
def tree(name, finder, partial = false, seen = {})
logical_name = name.gsub(%r|/_|, "/")
interpolated = name.include?("#")
if !interpolated && (template = find_template(finder, logical_name, [], partial, []))
2016-02-16 18:26:16 -05:00
if node = seen[template.identifier] # handle cycles in the tree
node
else
node = seen[template.identifier] = Node.create(name, logical_name, template, partial)
deps = DependencyTracker.find_dependencies(name, template, finder.view_paths)
deps.uniq { |n| n.gsub(%r|/_|, "/") }.each do |dep_file|
node.children << tree(dep_file, finder, true, seen)
end
node
2016-02-15 17:25:10 -05:00
end
else
unless interpolated # Dynamic template partial names can never be tracked
logger.error " Couldn't find template for digesting: #{name}"
end
seen[name] ||= Missing.new(name, logical_name, nil)
end
end
private
def find_template(finder, name, prefixes, partial, keys)
finder.disable_cache do
finder.find_all(name, prefixes, partial, keys).first
end
end
end
2016-02-16 17:25:34 -05:00
class Node
attr_reader :name, :logical_name, :template, :children
2016-02-15 17:32:41 -05:00
def self.create(name, logical_name, template, partial)
klass = partial ? Partial : Node
klass.new(name, logical_name, template, [])
2016-02-15 17:32:41 -05:00
end
def initialize(name, logical_name, template, children = [])
@name = name
2016-02-16 17:25:34 -05:00
@logical_name = logical_name
@template = template
@children = children
2016-02-15 17:32:41 -05:00
end
def digest(finder, stack = [])
ActiveSupport::Digest.hexdigest("#{template.source}-#{dependency_digest(finder, stack)}")
end
def dependency_digest(finder, stack)
2016-02-15 17:25:10 -05:00
children.map do |node|
if stack.include?(node)
false
else
finder.digest_cache[node.name] ||= begin
stack.push node
node.digest(finder, stack).tap { stack.pop }
end
2016-02-15 17:25:10 -05:00
end
end.join("-")
end
def to_dep_map
children.any? ? { name => children.map(&:to_dep_map) } : name
end
end
class Partial < Node; end
2016-02-15 17:25:10 -05:00
class Missing < Node
def digest(finder, _ = []) "" end
end
2016-02-15 17:25:10 -05:00
class Injected < Node
def digest(finder, _ = []) name end
2016-02-15 17:25:10 -05:00
end
class NullLogger
def self.debug(_); end
def self.error(_); end
end
end
end