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::ImmutableSortedSetOf, Contracts::PATH_MATCHER

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Contracts

#Contract

Constructor Details

#initialize(_vertices = {}) ⇒ Graph

Returns a new instance of Graph.


64
65
66
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 64

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

Instance Attribute Details

#graphObject (readonly)

Returns the value of attribute graph


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

def graph
  @graph
end

Instance Method Details

#add_edge(source, target) ⇒ Object


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

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


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

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


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

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


73
74
75
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 73

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

#invalidate_vertex!(vertex) ⇒ Object


68
69
70
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 68

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

#invalidatedObject


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

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)

141
142
143
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 141

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

#serializeObject


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
129
130
# File 'middleman-core/lib/middleman-core/dependencies/graph.rb', line 103

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