Class: CacheDigests::TemplateDigestor

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

Constant Summary collapse

EXPLICIT_DEPENDENCY =
/# Template Dependency: ([^ ]+)/
RENDER_DEPENDENCY =

Matches:

render partial: "comments/comment", collection: commentable.comments
render "comments/comments"
render 'comments/comments'
render('comments/comments')

render(@topic)         => render("topics/topic")
render(topics)         => render("topics/topic")
render(message.topics) => render("topics/topic")
/
  render\s*                     # render, followed by optional whitespace
  \(?                           # start an optional parenthesis for the render call
  (partial:|:partial\s+=>)?\s*  # naming the partial, used with collection -- 1st capture
  ([@a-z"'][@a-z_\/\."']+)      # the template name itself -- 2nd capture
/x

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, format, finder, options = {}) ⇒ TemplateDigestor

Returns a new instance of TemplateDigestor.



38
39
40
# File 'lib/cache_digests/template_digestor.rb', line 38

def initialize(name, format, finder, options = {})
  @name, @format, @finder, @options = name, format, finder, options
end

Instance Attribute Details

#finderObject (readonly)

Returns the value of attribute finder.



36
37
38
# File 'lib/cache_digests/template_digestor.rb', line 36

def finder
  @finder
end

#formatObject (readonly)

Returns the value of attribute format.



36
37
38
# File 'lib/cache_digests/template_digestor.rb', line 36

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.



36
37
38
# File 'lib/cache_digests/template_digestor.rb', line 36

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



36
37
38
# File 'lib/cache_digests/template_digestor.rb', line 36

def options
  @options
end

Class Method Details

.digest(name, format, finder, options = {}) ⇒ Object



30
31
32
33
34
# File 'lib/cache_digests/template_digestor.rb', line 30

def self.digest(name, format, finder, options = {})
  cache.fetch([ "digestor", cache_prefix, name, format ].compact.join("/")) do
    new(name, format, finder, options).digest
  end
end

Instance Method Details

#dependenciesObject



51
52
53
54
55
# File 'lib/cache_digests/template_digestor.rb', line 51

def dependencies
  render_dependencies + explicit_dependencies
rescue ActionView::MissingTemplate
  [] # File doesn't exist, so no dependencies
end

#digestObject



42
43
44
45
46
47
48
49
# File 'lib/cache_digests/template_digestor.rb', line 42

def digest
  Digest::MD5.hexdigest("#{source}-#{dependency_digest}").tap do |digest|
    logger.try :info, "Cache digest for #{name}.#{format}: #{digest}"
  end
rescue ActionView::MissingTemplate
  logger.try :error, "Couldn't find template for digesting: #{name}.#{format}"
  ''
end

#nested_dependenciesObject



57
58
59
60
61
62
# File 'lib/cache_digests/template_digestor.rb', line 57

def nested_dependencies
  dependencies.collect do |dependency|
    dependencies = TemplateDigestor.new(dependency, format, finder, partial: true).nested_dependencies
    dependencies.any? ? { dependency => dependencies } : dependency
  end
end