Permalink
Browse files

Nodes & Edges can now be initialized with another Node/Edge.

  • Loading branch information...
1 parent 1dad4cc commit 1e892e2bfae87de9cef41018f02b61a7b07275b2 @bfontaine committed Feb 13, 2013
Showing with 20 additions and 2 deletions.
  1. +4 −2 lib/graph.rb
  2. +8 −0 tests/edge_tests.rb
  3. +8 −0 tests/node_tests.rb
View
@@ -54,7 +54,9 @@ class Node
attr_accessor :attrs
def initialize(attrs=nil)
- @attrs = attrs || {}
+
+ @attrs = attrs.is_a?(Node) ? attrs.attrs : attrs || {}
+
end
# compare two nodes
@@ -86,7 +88,7 @@ class Edge
attr_accessor :attrs
def initialize(attrs=nil)
- @attrs = attrs || {}
+ @attrs = attrs.is_a?(Edge) ? attrs.attrs : attrs || {}
end
# compare two edges
View
@@ -43,4 +43,12 @@ def test_edge_update
assert_equal(true, e.update({}).is_a?(Graph::Edge))
end
+ def test_edge_init_with_another_edge
+
+ e = Graph::Edge.new({ :foo => 'bar' })
+
+ assert_equal( e, Graph::Edge.new(e) )
+
+ end
+
end
View
@@ -92,4 +92,12 @@ def test_node_update
assert_equal(true, n.update({}).is_a?(Graph::Node))
end
+ def test_node_init_with_another_node
+
+ n = Graph::Node.new({ :foo => 'bar' })
+
+ assert_equal( n, Graph::Node.new(n) )
+
+ end
+
end

0 comments on commit 1e892e2

Please sign in to comment.