diff options
| author | mo khan <mo@mokhan.ca> | 2015-04-08 12:38:38 -0600 |
|---|---|---|
| committer | mo khan <mo@mokhan.ca> | 2015-04-08 12:38:38 -0600 |
| commit | 12d901c86a7c03645379559605e47cb71db245dd (patch) | |
| tree | a8f955654dd25500223b50ea6a9e7d6594fb79b1 | |
| parent | 7527ff3c01108b0e06aeb8cbcd29e7f752415545 (diff) | |
extract xml_attributes method and move to node.
| -rw-r--r-- | lib/scale.rb | 1 | ||||
| -rw-r--r-- | lib/scale/node.rb | 10 | ||||
| -rw-r--r-- | lib/scale/shapes/circle.rb | 5 | ||||
| -rw-r--r-- | lib/scale/shapes/ellipse.rb | 13 | ||||
| -rw-r--r-- | lib/scale/shapes/rectangle.rb | 7 | ||||
| -rw-r--r-- | spec/node_spec.rb | 15 | ||||
| -rw-r--r-- | spec/shapes/circle_spec.rb | 4 | ||||
| -rw-r--r-- | spec/shapes/ellipse_spec.rb | 25 | ||||
| -rw-r--r-- | spec/shapes/rectangle_spec.rb | 4 |
9 files changed, 63 insertions, 21 deletions
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 |
