lib/nanoc/base/repos/dependency_store.rb in nanoc-4.4.3 vs lib/nanoc/base/repos/dependency_store.rb in nanoc-4.4.4
- old
+ new
@@ -1,50 +1,8 @@
module Nanoc::Int
# @api private
class DependencyStore < ::Nanoc::Int::Store
- # A dependency between two items/layouts.
- class Dependency
- include Nanoc::Int::ContractsSupport
-
- contract C::None => C::Or[Nanoc::Int::Item, Nanoc::Int::Layout]
- attr_reader :from
-
- contract C::None => C::Or[Nanoc::Int::Item, Nanoc::Int::Layout]
- attr_reader :to
-
- contract C::Maybe[C::Or[Nanoc::Int::Item, Nanoc::Int::Layout]], C::Maybe[C::Or[Nanoc::Int::Item, Nanoc::Int::Layout]], C::KeywordArgs[raw_content: C::Optional[C::Bool], attributes: C::Optional[C::Bool], compiled_content: C::Optional[C::Bool], path: C::Optional[C::Bool]] => C::Any
- def initialize(from, to, raw_content:, attributes:, compiled_content:, path:)
- @from = from
- @to = to
-
- @raw_content = raw_content
- @attributes = attributes
- @compiled_content = compiled_content
- @path = path
- end
-
- contract C::None => C::Bool
- def raw_content?
- @raw_content
- end
-
- contract C::None => C::Bool
- def attributes?
- @attributes
- end
-
- contract C::None => C::Bool
- def compiled_content?
- @compiled_content
- end
-
- contract C::None => C::Bool
- def path?
- @path
- end
- end
-
include Nanoc::Int::ContractsSupport
# @return [Array<Nanoc::Int::Item, Nanoc::Int::Layout>]
attr_accessor :objects
@@ -54,22 +12,24 @@
@objects = objects
@graph = Nanoc::Int::DirectedGraph.new([nil] + @objects)
end
- contract C::Or[Nanoc::Int::Item, Nanoc::Int::ItemRep, Nanoc::Int::Layout] => C::ArrayOf[Dependency]
+ contract C::Or[Nanoc::Int::Item, Nanoc::Int::ItemRep, Nanoc::Int::Layout] => C::ArrayOf[Nanoc::Int::Dependency]
def dependencies_causing_outdatedness_of(object)
objects_causing_outdatedness_of(object).map do |other_object|
- props = @graph.props_for(other_object, object) || {}
+ props = props_for(other_object, object)
- Dependency.new(
+ Nanoc::Int::Dependency.new(
other_object,
object,
- raw_content: props.fetch(:raw_content, false),
- attributes: props.fetch(:attributes, false),
- compiled_content: props.fetch(:compiled_content, false),
- path: props.fetch(:path, false),
+ Nanoc::Int::Props.new(
+ raw_content: props.fetch(:raw_content, false),
+ attributes: props.fetch(:attributes, false),
+ compiled_content: props.fetch(:compiled_content, false),
+ path: props.fetch(:path, false),
+ ),
)
end
end
# Returns the direct dependencies for the given object.
@@ -121,16 +81,16 @@
# dependency, i.e. the object that will cause the source to become
# outdated if the destination is outdated
#
# @return [void]
def record_dependency(src, dst, raw_content: false, attributes: false, compiled_content: false, path: false)
- existing_props = @graph.props_for(dst, src) || {}
- new_props = { raw_content: raw_content, attributes: attributes, compiled_content: compiled_content, path: path }
- props = merge_props(existing_props, new_props)
+ existing_props = Nanoc::Int::Props.new(@graph.props_for(dst, src) || {})
+ new_props = Nanoc::Int::Props.new(raw_content: raw_content, attributes: attributes, compiled_content: compiled_content, path: path)
+ props = existing_props.merge(new_props)
# Warning! dst and src are *reversed* here!
- @graph.add_edge(dst, src, props: props) unless src == dst
+ @graph.add_edge(dst, src, props: props.to_h) unless src == dst
end
# Empties the list of dependencies for the given object. This is necessary
# before recompiling the given object, because otherwise old dependencies
# will stick around and new dependencies will appear twice. This function
@@ -144,14 +104,17 @@
@graph.delete_edges_to(object)
end
protected
- def merge_props(p1, p2)
- keys = (p1.keys + p2.keys).uniq
- keys.each_with_object({}) do |key, memo|
- memo[key] = p1.fetch(key, false) || p2.fetch(key, false)
+ def props_for(a, b)
+ props = @graph.props_for(a, b) || {}
+
+ if props.values.any? { |v| v }
+ props
+ else
+ { raw_content: true, attributes: true, compiled_content: true, path: true }
end
end
def data
{
@@ -177,13 +140,14 @@
@graph.add_edge(from, to, props: props)
end
# Record dependency from all items on new items
new_objects = (@objects - previous_objects)
+ new_props = { raw_content: true, attributes: true, compiled_content: true, path: true }
new_objects.each do |new_obj|
@objects.each do |obj|
next unless obj.is_a?(Nanoc::Int::Item)
- @graph.add_edge(new_obj, obj)
+ @graph.add_edge(new_obj, obj, props: new_props)
end
end
end
end
end