summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormo khan <mo@mokhan.ca>2015-04-08 11:40:31 -0600
committermo khan <mo@mokhan.ca>2015-04-08 11:40:31 -0600
commit765509c40c7fc5f556151a0ef046fdec11e5e868 (patch)
tree819ab95d1cc8f7d2c9d8a77e7ef163c4998e5870
parentc7acbcacfc49dcc7b757b58c9eca7cb353c83cdf (diff)
add a child node.
-rw-r--r--lib/scale.rb1
-rw-r--r--lib/scale/rectangle.rb13
-rw-r--r--lib/scale/svg.rb8
-rw-r--r--spec/svg_spec.rb47
4 files changed, 53 insertions, 16 deletions
diff --git a/lib/scale.rb b/lib/scale.rb
index 27443fb..8c30b77 100644
--- a/lib/scale.rb
+++ b/lib/scale.rb
@@ -1,5 +1,6 @@
require "scale/version"
require "scale/svg"
+require "scale/rectangle"
module Scale
# Your code goes here... NOT!
diff --git a/lib/scale/rectangle.rb b/lib/scale/rectangle.rb
new file mode 100644
index 0000000..9e96bbd
--- /dev/null
+++ b/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
diff --git a/lib/scale/svg.rb b/lib/scale/svg.rb
index aa1ae45..f34b16b 100644
--- a/lib/scale/svg.rb
+++ b/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
diff --git a/spec/svg_spec.rb b/spec/svg_spec.rb
index 90cfab8..defc3de 100644
--- a/spec/svg_spec.rb
+++ b/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