Commit 765509c
Changed files (4)
lib
scale
spec
lib/scale/rectangle.rb
@@ -0,0 +1,13 @@
+module Scale
+ class Rectangle
+ def initialize(width: nil, height: nil, fill: nil)
+ @width = width
+ @height = height
+ @fill = fill
+ end
+
+ def append_to(builder)
+ builder.rect(width: @width, height: @height, fill: @fill)
+ end
+ end
+end
lib/scale/svg.rb
@@ -6,11 +6,19 @@ module Scale
def initialize(width: nil, height: nil)
@width, @height = width, height
+ @children = []
+ end
+
+ def add(node)
+ @children.push(node)
end
def to_xml
builder = Nokogiri::XML::Builder.new do |xml|
xml.svg(attributes) do
+ @children.each do |node|
+ node.append_to(xml)
+ end
end
end
builder.to_xml
lib/scale.rb
@@ -1,5 +1,6 @@
require "scale/version"
require "scale/svg"
+require "scale/rectangle"
module Scale
# Your code goes here... NOT!
spec/svg_spec.rb
@@ -1,29 +1,44 @@
describe Scale::SVG do
subject { Scale::SVG.new }
- it 'produces and empty xml document' do
- expected = <<-XML
+ describe "#to_xml" do
+ it 'produces and empty xml document' do
+ expected = <<-XML
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg\" version="1.1" baseProfile="full"/>
- XML
- expect(subject.to_xml).to eql(expected)
- end
+ XML
+ expect(subject.to_xml).to eql(expected)
+ end
- it 'applies a width' do
- subject.width = rand(1000)
- expected = <<-XML
+ it 'applies a width' do
+ subject.width = rand(1000)
+ expected = <<-XML
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg\" version="1.1" baseProfile="full" width="#{subject.width}"/>
- XML
- expect(subject.to_xml).to eql(expected)
- end
+ XML
+ expect(subject.to_xml).to eql(expected)
+ end
- it 'applies a height' do
- subject.height = rand(1000)
- expected = <<-XML
+ it 'applies a height' do
+ subject.height = rand(1000)
+ expected = <<-XML
<?xml version="1.0"?>
<svg xmlns="http://www.w3.org/2000/svg\" version="1.1" baseProfile="full" height="#{subject.height}"/>
- XML
- expect(subject.to_xml).to eql(expected)
+ XML
+ expect(subject.to_xml).to eql(expected)
+ end
+
+ context "when nesting elements" do
+ it 'produces the correct xml' do
+ subject.add(Scale::Rectangle.new(width: "100%", height: "100%", fill: "red"))
+ expected = <<-XML
+<?xml version="1.0"?>
+<svg xmlns="http://www.w3.org/2000/svg\" version="1.1" baseProfile="full">
+ <rect width="100%" height="100%" fill="red"/>
+</svg>
+ XML
+ expect(subject.to_xml).to eql(expected)
+ end
+ end
end
end