From 12d901c86a7c03645379559605e47cb71db245dd Mon Sep 17 00:00:00 2001 From: mo khan Date: Wed, 8 Apr 2015 12:38:38 -0600 Subject: extract xml_attributes method and move to node. --- lib/scale.rb | 1 + lib/scale/node.rb | 10 +++++++++- lib/scale/shapes/circle.rb | 5 ----- lib/scale/shapes/ellipse.rb | 13 +++++++++++++ lib/scale/shapes/rectangle.rb | 7 ------- spec/node_spec.rb | 15 +++++++++++++++ spec/shapes/circle_spec.rb | 4 ---- spec/shapes/ellipse_spec.rb | 25 +++++++++++++++++++++++++ spec/shapes/rectangle_spec.rb | 4 ---- 9 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 lib/scale/shapes/ellipse.rb create mode 100644 spec/node_spec.rb create mode 100644 spec/shapes/ellipse_spec.rb diff --git a/lib/scale.rb b/lib/scale.rb index 7a159aa..204a000 100644 --- a/lib/scale.rb +++ b/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! diff --git a/lib/scale/node.rb b/lib/scale/node.rb index 2f3f024..630c3c8 100644 --- a/lib/scale/node.rb +++ b/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 diff --git a/lib/scale/shapes/circle.rb b/lib/scale/shapes/circle.rb index edf8e6c..dc4db51 100644 --- a/lib/scale/shapes/circle.rb +++ b/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 diff --git a/lib/scale/shapes/ellipse.rb b/lib/scale/shapes/ellipse.rb new file mode 100644 index 0000000..fcd5ee0 --- /dev/null +++ b/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 diff --git a/lib/scale/shapes/rectangle.rb b/lib/scale/shapes/rectangle.rb index fb5b44e..8aaa01e 100644 --- a/lib/scale/shapes/rectangle.rb +++ b/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 diff --git a/spec/node_spec.rb b/spec/node_spec.rb new file mode 100644 index 0000000..f9cbb99 --- /dev/null +++ b/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 diff --git a/spec/shapes/circle_spec.rb b/spec/shapes/circle_spec.rb index e1fdeaa..0f80ca7 100644 --- a/spec/shapes/circle_spec.rb +++ b/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 diff --git a/spec/shapes/ellipse_spec.rb b/spec/shapes/ellipse_spec.rb new file mode 100644 index 0000000..0daa923 --- /dev/null +++ b/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 diff --git a/spec/shapes/rectangle_spec.rb b/spec/shapes/rectangle_spec.rb index cf718b8..d8b305b 100644 --- a/spec/shapes/rectangle_spec.rb +++ b/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 -- cgit v1.2.3