Class: ActionView::Digestor

Inherits:
Object
  • Object
show all
Defined in:
lib/action_view/digestor.rb

Direct Known Subclasses

PartialDigestor

Constant Summary collapse

@@cache =
ThreadSafe::Cache.new
@@digest_monitor =
Monitor.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Digestor.


47
48
49
# File 'lib/action_view/digestor.rb', line 47

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


45
46
47
# File 'lib/action_view/digestor.rb', line 45

def finder
  @finder
end

#formatObject (readonly)

Returns the value of attribute format


45
46
47
# File 'lib/action_view/digestor.rb', line 45

def format
  @format
end

#nameObject (readonly)

Returns the value of attribute name


45
46
47
# File 'lib/action_view/digestor.rb', line 45

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options


45
46
47
# File 'lib/action_view/digestor.rb', line 45

def options
  @options
end

Class Method Details

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


12
13
14
15
16
17
18
19
20
21
# File 'lib/action_view/digestor.rb', line 12

def digest(name, format, finder, options = {})
  cache_key = ([name, format] + Array.wrap(options[:dependencies])).join('.')
  # this is a correctly done double-checked locking idiom
  # (ThreadSafe::Cache's lookups have volatile semantics)
  @@cache[cache_key] || @@digest_monitor.synchronize do
    @@cache.fetch(cache_key) do # re-check under lock
      compute_and_store_digest(cache_key, name, format, finder, options)
    end
  end
end

Instance Method Details

#dependenciesObject


60
61
62
63
64
# File 'lib/action_view/digestor.rb', line 60

def dependencies
  DependencyTracker.find_dependencies(name, template)
rescue ActionView::MissingTemplate
  [] # File doesn't exist, so no dependencies
end

#digestObject


51
52
53
54
55
56
57
58
# File 'lib/action_view/digestor.rb', line 51

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


66
67
68
69
70
71
# File 'lib/action_view/digestor.rb', line 66

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