Permalink
Browse files

Add 'dup' and 'method_missing' to container class. Part of #6

  • Loading branch information...
1 parent 96b3d43 commit 48355782afc380a52d5d967191e6a652542cef7b @D4L committed Jan 2, 2013
@@ -8,10 +8,12 @@ module Composition
# class must implement #name
class UniqueNameCollection
attr_accessor :collection
-
end
+
end
end
require "abstract_graph/composition/uniquenamecollection/initialize"
require "abstract_graph/composition/uniquenamecollection/add"
+require "abstract_graph/composition/uniquenamecollection/dup"
+require "abstract_graph/composition/uniquenamecollection/method_missing"
@@ -0,0 +1,20 @@
+# required in "abstract_graph/composition/uniquenamecollection"
+
+module AbstractGraph
+ module Composition
+ class UniqueNameCollection
+
+ # does a deep copy of the object, in otherwords
+ # copies every object in the collection
+ def dup
+ other = UniqueNameCollection.new
+ # copy each object in our collection over
+ @collection.each_value do |o|
+ other.add o
+ end
+ other
+ end
+
+ end
+ end
+end
@@ -0,0 +1,14 @@
+# required in "abstract_graph/composition/uniquenamecollection"
+
+module AbstractGraph
+ module Composition
+ class UniqueNameCollection
+
+ # pass all methods into the hash
+ def method_missing( m, *args, &block )
+ @collection.send( m.to_sym, *args, &block )
+ end
+
+ end
+ end
+end
@@ -5,7 +5,6 @@ module Composition
describe UniqueNameCollection do
before(:each) do
-
@collection = UniqueNameCollection.new
end
@@ -0,0 +1,29 @@
+require 'spec_helper'
+
+module AbstractGraph
+ module Composition
+ describe UniqueNameCollection do
+
+ before :each do
+ @collection = UniqueNameCollection.new
+ end
+
+ describe "#dup" do
+
+ it "doesn't have the same id as the original" do
+ @collection.object_id.should_not == @collection.dup.object_id
+ end
+
+ it "creates a duplicate of every item in the collection" do
+ secondCollection = @collection.dup
+ dummy = Dummy.new
+ dummy.name = "Dummy"
+ @collection.add dummy
+ secondCollection.collection.size.should == 0
+ end
+
+ end
+
+ end
+ end
+end
@@ -0,0 +1,49 @@
+require 'spec_helper'
+module AbstractGraph
+ module Composition
+ describe UniqueNameCollection do
+
+ before :each do
+ @collection = UniqueNameCollection.new
+ @dummies = ('a'..'z').map do |c|
+ d = Dummy.new
+ d.name = c
+ d
+ end
+ @dummies.each do |d|
+ @collection.add d
+ end
+ end
+
+ describe "#size" do
+
+ it "should return the size of the collection" do
+ @collection.size.should == @dummies.count
+ end
+
+ end
+
+ describe "#has_key?(String)" do
+
+ it "should return whether the string is an included name" do
+ @collection.has_key?('d').should be_true
+ @collection.has_key?('saddy').should be_false
+ end
+
+ end
+
+ describe "#each_key" do
+
+ it "goes through every name in the collection" do
+ alphabet = ""
+ @collection.each_key do |c|
+ alphabet += c
+ end
+ alphabet.should == "abcdefghijklmnopqrstuvwxyz"
+ end
+
+ end
+
+ end
+ end
+end

0 comments on commit 4835578

Please sign in to comment.