/
checksum_store.rb
69 lines (56 loc) · 1.89 KB
/
checksum_store.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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_accessor :objects
# @param [Nanoc::Int::Site] site
def initialize(site: nil, objects:)
super(Nanoc::Int::Store.tmp_path_for(env_name: (site.config.env_name if site), store_name: 'checksums'), 1)
@site = site # TODO: Remove
@objects = objects
@checksums = {}
end
contract C::Any => C::Maybe[String]
# Returns the old checksum for the given object. This makes sense for
# items, layouts and code snippets.
#
# @param [#reference] obj The object for which to fetch the checksum
#
# @return [String] The checksum for the given object
def [](obj)
@checksums[obj.reference]
end
# Calculates and stores the checksum for the given object.
def add(obj)
if obj.is_a?(Nanoc::Int::Document)
@checksums[[obj.reference, :content]] = Nanoc::Int::Checksummer.calc_for_content_of(obj)
@checksums[[obj.reference, :attributes]] = Nanoc::Int::Checksummer.calc_for_attributes_of(obj)
end
@checksums[obj.reference] = Nanoc::Int::Checksummer.calc(obj)
end
contract C::Any => C::Maybe[String]
def content_checksum_for(obj)
@checksums[[obj.reference, :content]]
end
contract C::Any => C::Maybe[String]
def attributes_checksum_for(obj)
@checksums[[obj.reference, :attributes]]
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