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 an optional space
  \(?                      # start a optional parenthesis for the render call
  (partial:)?\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.



33
34
35
# File 'lib/cache_digests/template_digestor.rb', line 33

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.



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

def finder
  @finder
end

#formatObject (readonly)

Returns the value of attribute format.



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

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

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



27
28
29
# File 'lib/cache_digests/template_digestor.rb', line 27

def self.digest(name, format, finder, options = {})
  cache["#{name}.#{format}"] ||= new(name, format, finder, options).digest
end

Instance Method Details

#dependenciesObject



46
47
48
49
50
# File 'lib/cache_digests/template_digestor.rb', line 46

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

#digestObject



37
38
39
40
41
42
43
44
# File 'lib/cache_digests/template_digestor.rb', line 37

def digest
  Digest::MD5.hexdigest("#{name}.#{format}-#{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



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

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