deep_end 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,3 +1,3 @@
1
1
  module DeepEnd
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
data/lib/deep_end.rb CHANGED
@@ -5,12 +5,13 @@ module DeepEnd
5
5
  # Errors
6
6
  class SelfDependencyError < StandardError; end
7
7
  class CircularDependencyError < StandardError; end
8
-
8
+
9
9
  # Graph Node
10
10
  class Node
11
11
 
12
12
  attr_reader :key
13
13
  attr_accessor :seen
14
+ alias_method :seen?, :seen
14
15
  attr_reader :edges
15
16
 
16
17
  def initialize(key)
@@ -20,18 +21,16 @@ module DeepEnd
20
21
 
21
22
  def addEdge(node)
22
23
  @edges << node
23
- end
24
-
24
+ end
25
+
25
26
  end
26
27
 
27
28
  # Dependency Graph
28
29
  class Graph
29
30
 
30
31
  def resolved_dependencies
31
- a = []
32
- @resolved.each{|node| a << node.key}
33
- return a
34
- end
32
+ @resolved.map{|node| node.key}
33
+ end
35
34
 
36
35
  def initialize
37
36
  reset
@@ -47,70 +46,58 @@ module DeepEnd
47
46
  node.addEdge(node_for_key_or_new(dependency))
48
47
  end
49
48
  resolve_dependencies
50
- return @resolved
51
49
  end
52
50
 
53
51
  # Return the graph to its virgin state
54
52
  def reset
55
53
  @resolved = []
56
- @seen_this_pass
57
54
  @nodes = []
58
55
  end
59
56
 
60
57
  protected
61
58
 
62
- # Recurse through nodes
63
- def resolve_dependencies
64
- reset_seen
65
- @resolved = []
66
- @nodes.each do |node|
67
- @seen_this_pass = []
68
- resolve_dependency node unless node.seen
69
- end
59
+ # Recurse through nodes
60
+ def resolve_dependencies
61
+ reset_seen
62
+ @resolved = []
63
+ @nodes.each do |node|
64
+ @seen_this_pass = []
65
+ resolve_dependency node unless node.seen?
70
66
  end
67
+ @resolved
68
+ end
71
69
 
72
- # Recurse through node edges
73
- def resolve_dependency(node)
74
- node.seen = true
75
- @seen_this_pass << node
76
-
77
- node.edges.each do |edge|
78
- unless @resolved.include? edge
79
- unless @seen_this_pass.include? edge
80
- unless edge.seen
81
- resolve_dependency edge
82
- end
83
- else
84
- raise CircularDependencyError, "Circular reference detected: #{node.key.to_s} - #{edge.key.to_s}"
70
+ # Recurse through node edges
71
+ def resolve_dependency(node)
72
+ node.seen = true
73
+ @seen_this_pass << node
74
+
75
+ node.edges.each do |edge|
76
+ unless @resolved.include? edge
77
+ unless @seen_this_pass.include? edge
78
+ unless edge.seen?
79
+ resolve_dependency edge
85
80
  end
81
+ else
82
+ raise CircularDependencyError, "Circular reference detected: #{node.key.to_s} - #{edge.key.to_s}"
86
83
  end
87
84
  end
88
- @resolved << node
89
- end
90
-
91
- def key_exists(key)
92
- return node_for_key(key).present?
93
85
  end
86
+ @resolved << node
87
+ end
94
88
 
95
- def node_for_key(key)
96
- @nodes.each { |node| return node if node.key == key }
97
- return
98
- end
89
+ def node_for_key(key)
90
+ @nodes.each { |node| return node if node.key == key }
91
+ return
92
+ end
99
93
 
100
- def node_for_key_or_new(key)
101
- existing_node = node_for_key(key)
102
- if existing_node
103
- return existing_node
104
- else
105
- node = Node.new key
106
- @nodes << node
107
- return node
108
- end
109
- end
94
+ def node_for_key_or_new(key)
95
+ node_for_key(key) || (@nodes << Node.new(key)).last
96
+ end
110
97
 
111
- def reset_seen
112
- @nodes.each{ |node| node.seen = false}
113
- end
98
+ def reset_seen
99
+ @nodes.each{ |node| node.seen = false}
100
+ end
114
101
 
115
102
  end
116
- end
103
+ end
@@ -1,20 +1,20 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  module DeepEnd
4
-
4
+
5
5
  describe Graph do
6
6
 
7
7
  before(:each) do
8
8
  @graph = Graph.new
9
9
  end
10
10
 
11
- context 'when first created' do
11
+ context 'when first created' do
12
12
  it 'should have no resolved dependencies' do
13
13
  @graph.resolved_dependencies.should be_empty
14
14
  end
15
15
  end
16
16
 
17
- context 'when given dendencies' do
17
+ context 'when given dendencies' do
18
18
 
19
19
  # Create dependencies
20
20
  before(:each) do
@@ -61,34 +61,35 @@ module DeepEnd
61
61
 
62
62
  it 'should raise a SelfDependencyError if an object is added as its own dependency' do
63
63
  # Add dependencies
64
- expect { @graph.add_dependency @dependency_a, [@dependency_a] }.to raise_error(SelfDependencyError)
64
+ expect { @graph.add_dependency @dependency_a, [@dependency_a] }.to raise_error(SelfDependencyError)
65
65
  end
66
66
 
67
67
  it 'should raise a CircularDependencyError if objects are added with direct circular dependencies' do
68
68
  # Add dependencies
69
69
  @graph.add_dependency @dependency_b, [@dependency_a]
70
- expect { @graph.add_dependency @dependency_a, [@dependency_b] }.to raise_error(CircularDependencyError)
70
+ expect { @graph.add_dependency @dependency_a, [@dependency_b] }.to raise_error(CircularDependencyError)
71
71
  end
72
72
 
73
73
  it 'should raise a CircularDependencyError if objects are added with indirect circular dependencies' do
74
74
  # Add dependencies
75
75
  @graph.add_dependency @dependency_b, [@dependency_c]
76
76
  @graph.add_dependency @dependency_c, [@dependency_a]
77
- expect { @graph.add_dependency @dependency_a, [@dependency_b] }.to raise_error(CircularDependencyError)
77
+ expect { @graph.add_dependency @dependency_a, [@dependency_b] }.to raise_error(CircularDependencyError)
78
78
  end
79
79
 
80
- context "when reset" do
80
+ context "when reset" do
81
81
 
82
82
  it 'should have no resolved dependencies' do
83
83
  # Add dependencies
84
84
  @graph.add_dependency @dependency_c, [@dependency_b, @dependency_a]
85
85
  @graph.add_dependency @dependency_b, [@dependency_a]
86
86
  @graph.add_dependency @dependency_a
87
- @graph.reset
87
+ @graph.reset
88
88
  @graph.resolved_dependencies.length.should == 0
89
89
  end
90
+
90
91
  end
91
92
 
92
93
  end
93
94
  end
94
- end
95
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deep_end
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
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-07-05 00:00:00.000000000 Z
12
+ date: 2013-09-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: log4r
@@ -105,3 +105,4 @@ summary: This gem processes a list of objects and their dependencies, ordering t
105
105
  test_files:
106
106
  - spec/lib/deep_end_spec.rb
107
107
  - spec/spec_helper.rb
108
+ has_rdoc: