Commit 12d901c

mo khan <mo@mokhan.ca>
2015-04-08 18:38:38
extract xml_attributes method and move to node.
1 parent 7527ff3
lib/scale/shapes/circle.rb
@@ -1,7 +1,6 @@
 module Scale
   class Circle
     include Node
-    include Virtus.model
     attribute :cx, Integer
     attribute :cy, Integer
     attribute :r, Integer
@@ -9,9 +8,5 @@ module Scale
     def xml_tag
       :circle
     end
-
-    def attributes
-      super.delete_if { |key, value| value.nil? }
-    end
   end
 end
lib/scale/shapes/ellipse.rb
@@ -0,0 +1,13 @@
+module Scale
+  class Ellipse
+    include Node
+    attribute :rx, Integer
+    attribute :ry, Integer
+    attribute :cx, Integer
+    attribute :cy, Integer
+
+    def xml_tag
+      :ellipse
+    end
+  end
+end
lib/scale/shapes/rectangle.rb
@@ -1,9 +1,6 @@
-require 'virtus'
-
 module Scale
   class Rectangle
     include Node
-    include Virtus.model
     attribute :width, String
     attribute :height, String
     attribute :fill, String
@@ -15,9 +12,5 @@ module Scale
     def xml_tag
       :rect
     end
-
-    def attributes
-      super.delete_if { |key, value| value.nil? }
-    end
   end
 end
lib/scale/node.rb
@@ -1,5 +1,9 @@
+require 'virtus'
+
 module Scale
   module Node
+    include Virtus.module
+
     def children
       @children ||= []
     end
@@ -16,11 +20,15 @@ module Scale
     end
 
     def append_to(xml)
-      xml.send(xml_tag.to_sym, attributes) do
+      xml.send(xml_tag.to_sym, xml_attributes) do
         children.each do |node|
           node.append_to(xml)
         end
       end
     end
+
+    def xml_attributes
+      attributes.delete_if { |key, value| value.nil? }
+    end
   end
 end
lib/scale.rb
@@ -3,6 +3,7 @@ require "scale/node"
 require "scale/svg"
 require "scale/shapes/rectangle"
 require "scale/shapes/circle"
+require "scale/shapes/ellipse"
 
 module Scale
   # Your code goes here... NOT!
spec/shapes/circle_spec.rb
@@ -16,9 +16,5 @@ describe Scale::Circle do
       subject.cy = 10
       expect(subject.attributes).to include(cy: 10)
     end
-
-    it "skips attributes that are not specified" do
-      expect(subject.attributes).to be_empty
-    end
   end
 end
spec/shapes/ellipse_spec.rb
@@ -0,0 +1,25 @@
+describe Scale::Ellipse do
+  it { expect(subject.xml_tag).to eql(:ellipse) }
+
+  describe "#attributes" do
+    it "includes the x radius" do
+      subject.rx = 10
+      expect(subject.attributes).to include(rx: 10)
+    end
+
+    it "includes the y radius" do
+      subject.ry = 10
+      expect(subject.attributes).to include(ry: 10)
+    end
+
+    it 'includes the x position of the center' do
+      subject.cx = 10
+      expect(subject.attributes).to include(cx: 10)
+    end
+
+    it 'includes the y position of the center' do
+      subject.cy = 10
+      expect(subject.attributes).to include(cy: 10)
+    end
+  end
+end
spec/shapes/rectangle_spec.rb
@@ -31,9 +31,5 @@ describe Scale::Rectangle do
       subject.ry = 10
       expect(subject.attributes).to include(ry: 10)
     end
-
-    it "skips attributes that are not specified" do
-      expect(subject.attributes).to be_empty
-    end
   end
 end
spec/node_spec.rb
@@ -0,0 +1,15 @@
+describe Scale::Node do
+  class FakeNode
+    include Scale::Node
+    attribute :x, Integer
+    attribute :y, Integer
+  end
+
+  subject { FakeNode.new }
+
+  describe "#xml_attributes" do
+    it "skips attributes that are not specified" do
+      expect(subject.xml_attributes).to be_empty
+    end
+  end
+end