dag 0.0.3 → 0.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dag (0.0.2)
4
+ dag (0.0.3)
5
5
 
6
6
  GEM
7
7
  remote: http://rubygems.org/
data/lib/dag/vertex.rb CHANGED
@@ -43,6 +43,23 @@ class DAG
43
43
  successors.include?(other) || successors.any? {|v| v.has_path_to?(other) }
44
44
  end
45
45
 
46
+ alias :has_descendent? :has_path_to?
47
+
48
+ #
49
+ # Is there a path from +other+ to here following edges in the DAG?
50
+ #
51
+ # @param [DAG::Vertex] another Vertex is the same DAG
52
+ # @raise [ArgumentError] if +other+ is not a Vertex in the same DAG
53
+ # @return true iff there is a path following edges within this DAG
54
+ #
55
+ def has_ancestor?(other)
56
+ raise ArgumentError.new('You must supply a vertex in this DAG') unless
57
+ other && Vertex === other && other.dag == self.dag
58
+ predecessors.include?(other) || predecessors.any? {|v| v.has_ancestor?(other) }
59
+ end
60
+
61
+ alias :is_reachable_from? :has_ancestor?
62
+
46
63
  #
47
64
  # Retrieve a value from the vertex's payload.
48
65
  # This is a shortcut for vertex.payload[key].
@@ -5,8 +5,9 @@ describe DAG::Vertex do
5
5
  subject { dag.add_vertex }
6
6
  let(:v1) { dag.add_vertex(name: :v1) }
7
7
  let(:v2) { dag.add_vertex(name: :v2) }
8
+ let(:v3) { dag.add_vertex(name: 'v3') }
8
9
 
9
- describe 'an instance' do
10
+ describe '#has_path_to?' do
10
11
  it 'cannot have a path to a non-vertex' do
11
12
  expect { subject.has_path_to?(23) }.to raise_error(ArgumentError)
12
13
  end
@@ -16,6 +17,16 @@ describe DAG::Vertex do
16
17
  end
17
18
  end
18
19
 
20
+ describe '#has_ancestor?' do
21
+ it 'ancestors must be a vertex' do
22
+ expect { subject.has_ancestor?(23) }.to raise_error(ArgumentError)
23
+ end
24
+
25
+ it 'ancestors must be in the same DAG' do
26
+ expect { subject.has_ancestor?(DAG.new.add_vertex) }.to raise_error(ArgumentError)
27
+ end
28
+ end
29
+
19
30
  describe 'with a payload' do
20
31
  subject { dag.add_vertex(name: 'Fred', size: 34) }
21
32
 
@@ -55,6 +66,12 @@ describe DAG::Vertex do
55
66
  subject.predecessors.should == [v1, v2]
56
67
  end
57
68
  end
69
+
70
+ it 'has the correct ancestors' do
71
+ subject.has_ancestor?(v1).should be_true
72
+ subject.has_ancestor?(v2).should be_true
73
+ subject.has_ancestor?(v3).should be_false
74
+ end
58
75
  end
59
76
 
60
77
  context 'with successors' do
@@ -77,11 +94,14 @@ describe DAG::Vertex do
77
94
  subject.successors.should == [v1, v2]
78
95
  end
79
96
  end
97
+
98
+ it 'has no ancestors' do
99
+ subject.has_ancestor?(v1).should be_false
100
+ subject.has_ancestor?(v2).should be_false
101
+ end
80
102
  end
81
103
 
82
104
  context 'in a deep DAG' do
83
- let(:v3) { dag.add_vertex }
84
-
85
105
  before do
86
106
  dag.add_edge from: subject, to: v1
87
107
  dag.add_edge from: v1, to: v2
@@ -94,6 +114,10 @@ describe DAG::Vertex do
94
114
  it 'has no path to v3' do
95
115
  subject.has_path_to?(v3).should be_false
96
116
  end
117
+
118
+ it 'recognises that it is an ancestor of v2' do
119
+ v2.has_ancestor?(subject).should be_true
120
+ end
97
121
  end
98
122
 
99
123
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dag
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-07 00:00:00.000000000 Z
12
+ date: 2013-04-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake