Sha256: f1a71d1c6a9dd317e65c29044425beb0b9055924d603fc4cce22220d64fa425b

Contents?: true

Size: 1.89 KB

Versions: 15

Compression:

Stored size: 1.89 KB

Contents

# frozen_string_literal: true

module Nanoc::Int
  # Stores checksums for objects in order to be able to detect whether a file
  # has changed since the last site compilation.
  #
  # @api private
  class ChecksumStore < ::Nanoc::Int::Store
    include Nanoc::Int::ContractsSupport

    attr_writer :checksums
    attr_accessor :objects

    c_obj = C::Or[Nanoc::Int::Item, Nanoc::Int::Layout, Nanoc::Int::Configuration, Nanoc::Int::CodeSnippet]

    contract C::KeywordArgs[site: C::Maybe[Nanoc::Int::Site], objects: C::IterOf[c_obj]] => C::Any
    def initialize(site: nil, objects:)
      super(Nanoc::Int::Store.tmp_path_for(site: site, store_name: 'checksums'), 2)

      @objects = objects

      @checksums = {}
    end

    contract c_obj => C::Maybe[String]
    def [](obj)
      @checksums[obj.reference]
    end

    contract c_obj => self
    def add(obj)
      if obj.is_a?(Nanoc::Int::Document)
        @checksums[[obj.reference, :content]] = Nanoc::Int::Checksummer.calc_for_content_of(obj)
      end

      if obj.is_a?(Nanoc::Int::Document) || obj.is_a?(Nanoc::Int::Configuration)
        @checksums[[obj.reference, :each_attribute]] = Nanoc::Int::Checksummer.calc_for_each_attribute_of(obj)
      end

      @checksums[obj.reference] = Nanoc::Int::Checksummer.calc(obj)

      self
    end

    contract c_obj => C::Maybe[String]
    def content_checksum_for(obj)
      @checksums[[obj.reference, :content]]
    end

    contract c_obj => C::Maybe[C::HashOf[Symbol, String]]
    def attributes_checksum_for(obj)
      @checksums[[obj.reference, :each_attribute]]
    end

    protected

    def data
      @checksums
    end

    def data=(new_data)
      references = Set.new(@objects.map(&:reference))

      @checksums = {}
      new_data.each_pair do |key, checksum|
        if references.include?(key) || references.include?(key.first)
          @checksums[key] = checksum
        end
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
nanoc-4.8.12 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.11 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.10 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.9 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.8 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.7 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.6 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.5 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.4 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.3 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.2 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.1 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.8.0 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.7.14 lib/nanoc/base/repos/checksum_store.rb
nanoc-4.7.13 lib/nanoc/base/repos/checksum_store.rb