Class: Nanoc::Core::CompilationItemRepView

Inherits:
BasicItemRepView show all
Defined in:
lib/nanoc/core/views/compilation_item_rep_view.rb

Instance Method Summary collapse

Methods inherited from BasicItemRepView

#==, #_unwrap, #binary?, #eql?, #hash, #initialize, #inspect, #item, #name, #path, #snapshot?

Methods inherited from View

#_context, #_unwrap, #frozen?, #initialize, #inspect

Methods included from ContractsSupport

enabled?, included, setup_once, warn_about_performance

Constructor Details

This class inherits a constructor from Nanoc::Core::BasicItemRepView

Instance Method Details

#_try_load_from_cacheObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nanoc/core/views/compilation_item_rep_view.rb', line 70

def _try_load_from_cache
  # If we get an unmet dependency, try to load the content from the
  # compiled content cache. If this is not possible, re-raise the unmet
  # dependency error, and then let the compiler deal with it regularly.

  compilation_context = @context.compilation_context
  compiled_content_cache = compilation_context.compiled_content_cache
  compiled_content_repo = compilation_context.compiled_content_repo

  # Requirement: The item rep must not be marked as outdated.
  outdated = compilation_context.outdatedness_store.include?(@item_rep)
  return false if outdated

  # Requirement: The compiled content cache must have a cache entry for
  # this item rep.
  cache_available =
    compiled_content_cache.full_cache_available?(@item_rep)
  return false unless cache_available

  # Load the compiled content from the cache
  Nanoc::Core::NotificationCenter.post(:cached_content_used, @item_rep)
  compiled_content_repo.set_all(
    @item_rep,
    compiled_content_cache[@item_rep],
  )

  # Mark as compiled
  @item_rep.compiled = true

  true
end

#compiled_content(snapshot: nil) ⇒ String

Returns the compiled content.

Parameters:

  • snapshot (String) (defaults to: nil)

    The name of the snapshot from which to fetch the compiled content. By default, the returned compiled content will be the content compiled right before the first layout call (if any).

Returns:

  • (String)

    The content at the given snapshot.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/nanoc/core/views/compilation_item_rep_view.rb', line 48

def compiled_content(snapshot: nil)
  compiled_content_repo = @context.compiled_content_repo

  @context.dependency_tracker.bounce(
    @item_rep.item,
    compiled_content: true,
  )

  begin
    compiled_content_repo.compiled_content(rep: @item_rep, snapshot:)
  rescue Nanoc::Core::Errors::UnmetDependency => e
    could_load = _try_load_from_cache
    unless could_load
      raise e
    end

    # Get the compiled content again. Previously in this method, this is
    # what raised the `UnmetDependency` error.
    compiled_content_repo.compiled_content(rep: @item_rep, snapshot:)
  end
end

#item_view_classObject

This method is abstract.


7
8
9
# File 'lib/nanoc/core/views/compilation_item_rep_view.rb', line 7

def item_view_class
  Nanoc::Core::CompilationItemView
end

#raw_path(snapshot: :last) ⇒ String

Returns the item rep’s raw path. It includes the path to the output directory and the full filename.

Parameters:

  • snapshot (Symbol) (defaults to: :last)

    The snapshot for which the path should be returned.

Returns:

  • (String)

    The item rep’s raw path.



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/nanoc/core/views/compilation_item_rep_view.rb', line 18

def raw_path(snapshot: :last)
  @context.dependency_tracker.bounce(_unwrap.item, compiled_content: true)

  raw_path = @item_rep.raw_path(snapshot:)

  unless @item_rep.compiled?
    could_load = _try_load_from_cache

    unless could_load
      raise Nanoc::Core::Errors::UnmetDependency.new(@item_rep, snapshot)
    end
  end

  # Ensure file exists
  if raw_path && !File.file?(raw_path)
    raise Nanoc::Core::Errors::InternalInconsistency,
          "File `#{raw_path}` expected to exist, but did not."
  end

  raw_path
end