Class: Middleman::Dependencies::Graph

Inherits:
Object
  • Object
show all
Includes:
Contracts
Defined in:
middleman-core/lib/middleman-core/dependencies/graph.rb

Constant Summary

Constants included from Contracts

Contracts::ImmutableSetOf, Contracts::ImmutableSortedSetOf, Contracts::OldResourceList, Contracts::PATH_MATCHER, Contracts::ResourceList, Contracts::VectorOf

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

#initialize(_vertices = {}) ⇒ Graph

Returns a new instance of Graph



62
63
64
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 62

def initialize(_vertices = {})
  @graph = DirectedAdjacencyGraph.new
end

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph



60
61
62
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 60

def graph
  @graph
end

Instance Method Details

#add_edge(source, target) ⇒ Object



76
77
78
79
80
81
82
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 76

def add_edge(source, target)
  if source == target
    add_vertex(source)
  else
    @graph.add_edge(source, target)
  end
end

#add_edge_by_key(source, target) ⇒ Object



85
86
87
88
89
90
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 85

def add_edge_by_key(source, target)
  a = @graph.find_vertex_by_key(source)
  b = @graph.find_vertex_by_key(target)

  add_edge(a, b)
end

#add_edge_set(edge) ⇒ Object



93
94
95
96
97
98
99
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 93

def add_edge_set(edge)
  return if edge.depends_on.nil?

  edge.depends_on.each do |depended_on|
    add_edge(depended_on, edge.vertex)
  end
end

#add_vertex(vertex) ⇒ Object



71
72
73
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 71

def add_vertex(vertex)
  @graph.add_vertex(vertex)
end

#invalidate_vertex!(vertex) ⇒ Object



66
67
68
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 66

def invalidate_vertex!(vertex)
  @graph.remove_vertex(vertex)
end

#invalidatedObject



131
132
133
134
135
136
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 131

def invalidated
  invalidated = @graph.vertices.reject(&:valid?)
  invalidated.each { |v| @graph.remove_vertex(v) }

  ::Hamster::Set.new(invalidated)
end

#invalidates_resource?(resource) ⇒ Boolean

Returns:

  • (Boolean)


139
140
141
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 139

def invalidates_resource?(resource)
  @graph.vertices.none? { |d| d.matches_resource?(resource) }
end

#serializeObject



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 101

def serialize
  edges = @graph.edges
                .sort_by { |edge| edge.target.key }
                .each_with_object({}) do |edge, sum|
    sum[edge.target.key.to_s] ||= []
    sum[edge.target.key.to_s] << edge.source.key.to_s
  end

  # Sort dependencies list
  edges = edges.each { |(k, v)| edges[k] = v.sort }

  vertices = @graph.vertices
                   .sort_by { |v| [v.type_id, v.key] }
                   .each_with_object({}) do |v, sum|
    s = v.serialize
    k = s.delete 'key'
    t = s.delete 'type'
    a = s.delete 'attrs'

    sum[t] ||= {}
    sum[t][k] = a['hash']
  end

  {
    'edges' => edges,
    'vertices' => vertices
  }
end