dag 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.yardopts ADDED
@@ -0,0 +1 @@
1
+ --no-private
data/README.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Very simple directed acyclic graphs for Ruby.
4
4
 
5
+ [![Build Status](https://travis-ci.org/kevinrutherford/dag.png)](https://travis-ci.org/kevinrutherford/dag)
6
+ [![Code
7
+ Climate](https://codeclimate.com/github/kevinrutherford/dag.png)](https://codeclimate.com/github/kevinrutherford/dag)
8
+
5
9
  ## Installation
6
10
 
7
11
  Install the gem
data/lib/dag/vertex.rb CHANGED
@@ -1,12 +1,15 @@
1
1
  class DAG
2
2
 
3
3
  class Vertex
4
- attr_reader :dag
4
+ attr_reader :dag, :payload
5
5
 
6
- def initialize(dag)
6
+ def initialize(dag, payload)
7
7
  @dag = dag
8
+ @payload = payload
8
9
  end
9
10
 
11
+ private :initialize
12
+
10
13
  def outgoing_edges
11
14
  @dag.edges.select {|e| e.origin == self}
12
15
  end
@@ -23,11 +26,34 @@ class DAG
23
26
  outgoing_edges.map(&:destination).uniq
24
27
  end
25
28
 
29
+ def inspect
30
+ "DAG::Vertex:#{@payload.inspect}"
31
+ end
32
+
33
+ #
34
+ # Is there a path from here to +other+ following edges in the DAG?
35
+ #
36
+ # @param [DAG::Vertex] another Vertex is the same DAG
37
+ # @raise [ArgumentError] if +other+ is not a Vertex in the same DAG
38
+ # @return true iff there is a path following edges within this DAG
39
+ #
26
40
  def has_path_to?(other)
27
41
  raise ArgumentError.new('You must supply a vertex in this DAG') unless
28
42
  other && Vertex === other && other.dag == self.dag
29
43
  successors.include?(other) || successors.any? {|v| v.has_path_to?(other) }
30
44
  end
45
+
46
+ #
47
+ # Retrieve a value from the vertex's payload.
48
+ # This is a shortcut for vertex.payload[key].
49
+ #
50
+ # @param key [Object] the payload key
51
+ # @return the corresponding value from the payload Hash, or nil if not found
52
+ #
53
+ def [](key)
54
+ @payload[key]
55
+ end
56
+
31
57
  end
32
58
 
33
59
  end
data/lib/dag.rb CHANGED
@@ -11,8 +11,8 @@ class DAG
11
11
  @edges = []
12
12
  end
13
13
 
14
- def add_vertex
15
- Vertex.new(self).tap {|v| @vertices << v }
14
+ def add_vertex(payload = {})
15
+ Vertex.new(self, payload).tap {|v| @vertices << v }
16
16
  end
17
17
 
18
18
  def add_edge(attrs)
@@ -3,8 +3,8 @@ require 'spec_helper'
3
3
  describe DAG::Vertex do
4
4
  let(:dag) { DAG.new }
5
5
  subject { dag.add_vertex }
6
- let(:v1) { dag.add_vertex }
7
- let(:v2) { dag.add_vertex }
6
+ let(:v1) { dag.add_vertex(name: :v1) }
7
+ let(:v2) { dag.add_vertex(name: :v2) }
8
8
 
9
9
  describe 'an instance' do
10
10
  it 'cannot have a path to a non-vertex' do
@@ -16,6 +16,25 @@ describe DAG::Vertex do
16
16
  end
17
17
  end
18
18
 
19
+ describe 'with a payload' do
20
+ subject { dag.add_vertex(name: 'Fred', size: 34) }
21
+
22
+ it 'allows the payload to be accessed' do
23
+ subject[:name].should == 'Fred'
24
+ subject[:size].should == 34
25
+ subject.payload.should == {name: 'Fred', size: 34}
26
+ end
27
+
28
+ it 'returns nil for missing payload key' do
29
+ subject[56].should be_nil
30
+ end
31
+
32
+ it 'allows the payload to be changed' do
33
+ subject.payload[:another] = 'ha'
34
+ subject[:another].should == 'ha'
35
+ end
36
+ end
37
+
19
38
  context 'with predecessors' do
20
39
  before do
21
40
  dag.add_edge from: v1, to: subject
data/spec/lib/dag_spec.rb CHANGED
@@ -102,9 +102,5 @@ describe DAG do
102
102
 
103
103
  end
104
104
 
105
- context 'vertex attributes'
106
-
107
- context 'edge attributes'
108
-
109
105
  end
110
106
 
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.1
4
+ version: 0.0.2
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-05 00:00:00.000000000 Z
12
+ date: 2013-04-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rake
@@ -66,6 +66,7 @@ extensions: []
66
66
  extra_rdoc_files: []
67
67
  files:
68
68
  - .rspec
69
+ - .yardopts
69
70
  - Gemfile
70
71
  - Gemfile.lock
71
72
  - README.md