Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdefreyne committed Jun 11, 2017
1 parent c42b115 commit 5e9d550
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
25 changes: 25 additions & 0 deletions lib/nanoc/base/entities/directed_graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def initialize(vertices)
@roots = Set.new(@vertices.keys)

invalidate_caches
check_invariants
end

# @group Modifying the graph
Expand Down Expand Up @@ -76,6 +77,7 @@ def add_edge(from, to, props: nil)
@roots.delete(to)

invalidate_caches
check_invariants
end

# Removes the edge from the first vertex to the second vertex. If the
Expand All @@ -98,6 +100,7 @@ def delete_edge(from, to)
@roots.add(to) if @to_graph[to].empty?

invalidate_caches
check_invariants
end

# Adds the given vertex to the graph.
Expand All @@ -111,6 +114,8 @@ def add_vertex(v)
@vertices[v] = @vertices.size

@roots << v

check_invariants
end

# Deletes all edges coming from the given vertex.
Expand All @@ -127,6 +132,8 @@ def delete_edges_from(from)
@roots.add(to) if @to_graph[to].empty?
end
@from_graph.delete(from)

check_invariants
end

# Deletes all edges going to the given vertex.
Expand All @@ -143,6 +150,20 @@ def delete_edges_to(to)
end
@to_graph.delete(to)
@roots.add(to)

check_invariants
end

def check_invariants
@vertices.each do |(k, v)|
unless v
raise "Internal inconsistency: idx for vertex #{k.inspect} is nil (I)"
end

unless @vertices[k]
raise "Internal inconsistency: idx for vertex #{k.inspect} is nil (II)"
end
end
end

# Removes the given vertex from the graph.
Expand All @@ -156,6 +177,8 @@ def delete_vertex(v)

@vertices.delete(v)
@roots.delete(v)

check_invariants
end

# @group Querying the graph
Expand Down Expand Up @@ -246,6 +269,8 @@ def props_for(from, to)

# @return [Array] The list of all vertices in this graph.
def vertices
check_invariants

@vertices.keys.sort_by { |v| @vertices[v] }
end

Expand Down
5 changes: 0 additions & 5 deletions lib/nanoc/base/entities/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,6 @@ def inspect
"<#{self.class} identifier=\"#{identifier}\">"
end

contract C::None => C::Num
def hash
self.class.hash ^ identifier.hash
end

contract C::Any => C::Bool
def ==(other)
other.respond_to?(:identifier) && identifier == other.identifier
Expand Down
8 changes: 0 additions & 8 deletions spec/nanoc/base/entities/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -178,21 +178,13 @@
let(:identifier_arg_b) { '/home.md' }

it { is_expected.to eql(true) }

it 'has same hashes' do
expect(document_a.hash).to eql(document_b.hash)
end
end

context 'different identifier' do
let(:identifier_arg_a) { '/home.md' }
let(:identifier_arg_b) { '/about.md' }

it { is_expected.to eql(false) }

it 'has different hashes' do
expect(document_a.hash).not_to eql(document_b.hash)
end
end

context 'comparing with non-document' do
Expand Down

0 comments on commit 5e9d550

Please sign in to comment.