Sha256: 6471ce01b91f753caefb0d2acdf0fba8193ec6a7a10a467aa66aa8fd0b9c59d2

Contents?: true

Size: 1.3 KB

Versions: 41

Compression:

Stored size: 1.3 KB

Contents

# frozen_string_literal: true

module Nanoc::Int::Compiler::Phases
  # Provides functionality for (re)calculating the content of an item rep, with caching or
  # outdatedness checking. Delegates to s::Recalculate if outdated or no cache available.
  class Cache < Abstract
    include Nanoc::Int::ContractsSupport

    def initialize(wrapped:, compiled_content_cache:, snapshot_repo:)
      super(wrapped: wrapped)

      @compiled_content_cache = compiled_content_cache
      @snapshot_repo = snapshot_repo
    end

    contract Nanoc::Int::ItemRep, C::KeywordArgs[is_outdated: C::Bool], C::Func[C::None => C::Any] => C::Any
    def run(rep, is_outdated:)
      if can_reuse_content_for_rep?(rep, is_outdated: is_outdated)
        Nanoc::Int::NotificationCenter.post(:cached_content_used, rep)

        @snapshot_repo.set_all(rep, @compiled_content_cache[rep])
      else
        yield
      end

      rep.compiled = true
      @compiled_content_cache[rep] = @snapshot_repo.get_all(rep)
    end

    contract Nanoc::Int::ItemRep, C::KeywordArgs[is_outdated: C::Bool] => C::Bool
    def can_reuse_content_for_rep?(rep, is_outdated:)
      if is_outdated
        false
      else
        cache = @compiled_content_cache[rep]
        cache ? cache.none? { |_snapshot_name, content| content.binary? } : false
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 1 rubygems

Version Path
nanoc-4.11.0 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.10.4 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.10.3 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.10.2 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.10.1 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.10.0 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.9.9 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.9.8 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.9.7 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.9.6 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.9.5 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.9.4 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.9.3 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.9.2 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.9.1 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.9.0 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.8.19 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.8.18 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.8.17 lib/nanoc/base/services/compiler/phases/cache.rb
nanoc-4.8.16 lib/nanoc/base/services/compiler/phases/cache.rb