From 353a5d54ad4014af126c76617d9b02029a3ee98c Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 10 Feb 2012 16:05:21 +0000 Subject: [PATCH] Added hash parameter to graph initialization --- Gemfile | 7 +++---- Gemfile.lock | 33 +++++++++++++++++---------------- lib/graph.njae/edge.rb | 4 ++-- lib/graph.njae/graph.rb | 21 ++++++++++++++++++--- lib/graph.njae/vertex.rb | 4 ++-- spec/graph/edge_spec.rb | 9 +++++++++ spec/graph/graph_spec.rb | 9 +++++++++ spec/graph/vertex_spec.rb | 8 ++++++++ 8 files changed, 68 insertions(+), 27 deletions(-) diff --git a/Gemfile b/Gemfile index d6b498f..f677f3d 100644 --- a/Gemfile +++ b/Gemfile @@ -6,9 +6,8 @@ source "http://rubygems.org" # Add dependencies to develop your gem here. # Include everything needed to run rake, tests, features, etc. group :development do - gem "rspec", "~> 2.6.0" - gem "bundler", "~> 1.0.0" - gem "jeweler", "~> 1.6.2" - gem "rcov", ">= 0" + gem "rspec" + gem "bundler" + gem "jeweler" gem "rdoc" end diff --git a/Gemfile.lock b/Gemfile.lock index 024a43a..9b1b95b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,30 +1,31 @@ GEM remote: http://rubygems.org/ specs: - diff-lcs (1.1.2) + diff-lcs (1.1.3) git (1.2.5) - jeweler (1.6.2) + jeweler (1.8.3) bundler (~> 1.0) git (>= 1.2.5) rake - rake (0.9.2) - rcov (0.9.9) - rdoc (3.7) - rspec (2.6.0) - rspec-core (~> 2.6.0) - rspec-expectations (~> 2.6.0) - rspec-mocks (~> 2.6.0) - rspec-core (2.6.4) - rspec-expectations (2.6.0) + rdoc + json (1.6.5) + rake (0.9.2.2) + rdoc (3.12) + json (~> 1.4) + rspec (2.8.0) + rspec-core (~> 2.8.0) + rspec-expectations (~> 2.8.0) + rspec-mocks (~> 2.8.0) + rspec-core (2.8.0) + rspec-expectations (2.8.0) diff-lcs (~> 1.1.2) - rspec-mocks (2.6.0) + rspec-mocks (2.8.0) PLATFORMS ruby DEPENDENCIES - bundler (~> 1.0.0) - jeweler (~> 1.6.2) - rcov + bundler + jeweler rdoc - rspec (~> 2.6.0) + rspec diff --git a/lib/graph.njae/edge.rb b/lib/graph.njae/edge.rb index f38a32b..814360b 100644 --- a/lib/graph.njae/edge.rb +++ b/lib/graph.njae/edge.rb @@ -10,8 +10,8 @@ module GraphNjae # Each connection is handled by a Graph::Connection object, so that each end # of the Edge can have it's own attributes. class Edge < OpenStruct - def initialize - super + def initialize(values = {}) + super(values) self.connections = [] self end diff --git a/lib/graph.njae/graph.rb b/lib/graph.njae/graph.rb index e08a1ad..b1de59b 100644 --- a/lib/graph.njae/graph.rb +++ b/lib/graph.njae/graph.rb @@ -7,8 +7,8 @@ module GraphNjae # A container for all the parts of a graph. The graph can have arbitrary attributes, # treated as method names. class Graph < OpenStruct - def initialize - super + def initialize(values = {}) + super(values) self.edges = Array.new self.vertices = Array.new self @@ -23,5 +23,20 @@ module GraphNjae end self end - end + + # Form a product graph of this graph and the other. + # Return the new graph. + def product(other) + product_graph = Graph.new + self.vertices.each do |v1| + other.vertices.each do |v2| + product_vertex = Vertex.new + product_vertex.left_node = v1 + product_vertex.right_node = v2 + product_graph << product_vertex + end + end + end + + end # class end diff --git a/lib/graph.njae/vertex.rb b/lib/graph.njae/vertex.rb index 4a75b6d..6b34351 100644 --- a/lib/graph.njae/vertex.rb +++ b/lib/graph.njae/vertex.rb @@ -6,8 +6,8 @@ module GraphNjae # A vertex in a graph. The edge can have arbitrary attributes,treated as # method names. class Vertex < OpenStruct - def initialize - super + def initialize(values = {}) + super(values) self.edges = [] self end diff --git a/spec/graph/edge_spec.rb b/spec/graph/edge_spec.rb index 2e7dbfc..d139667 100644 --- a/spec/graph/edge_spec.rb +++ b/spec/graph/edge_spec.rb @@ -8,6 +8,15 @@ module GraphNjae e = Edge.new e.connections.should be_empty end + + it "creates an edge with some parameters" do + e = Edge.new :value1 => 1, :value2 => "value2", :value3 => :v3 + e.value1.should == 1 + e.value2.should == "value2" + e.value3.should == :v3 + e.value4.should be_nil + end + end # #initialize describe "adds attribues" do diff --git a/spec/graph/graph_spec.rb b/spec/graph/graph_spec.rb index 09cc6a4..5da61f5 100644 --- a/spec/graph/graph_spec.rb +++ b/spec/graph/graph_spec.rb @@ -16,6 +16,15 @@ module GraphNjae g.edges.should be_empty g.vertices.should be_empty end + + it "creates a graph with some parameters" do + g = Graph.new :value1 => 1, :value2 => "value2", :value3 => :v3 + g.value1.should == 1 + g.value2.should == "value2" + g.value3.should == :v3 + g.value4.should be_nil + end + end # #initialize describe "adds attribues" do diff --git a/spec/graph/vertex_spec.rb b/spec/graph/vertex_spec.rb index 49ce58c..b193bc8 100644 --- a/spec/graph/vertex_spec.rb +++ b/spec/graph/vertex_spec.rb @@ -9,6 +9,14 @@ module GraphNjae v = Vertex.new v.edges.should be_empty end + + it "creates a vertex with some parameters" do + v = Vertex.new :value1 => 1, :value2 => "value2", :value3 => :v3 + v.value1.should == 1 + v.value2.should == "value2" + v.value3.should == :v3 + v.value4.should be_nil + end end # #initialize describe "adds attribues" do -- 2.34.1