Commit 55a6d4b

mo khan <mo@mokhan.ca>
2015-04-08 19:08:02
first cut at a text node.
1 parent 37fa83f
Changed files (4)
lib/scale/node.rb
@@ -4,6 +4,7 @@ module Scale
   module Node
     include Enumerable
     include Virtus.module
+    attr_reader :content
 
     def children
       @children ||= []
@@ -26,10 +27,18 @@ module Scale
       builder.to_xml
     end
 
-    def append_to(xml)
-      xml.send(xml_tag.to_sym, xml_attributes) do
-        each do |node|
-          node.append_to(xml)
+    def append_to(builder)
+      if content.nil?
+        builder.send(xml_tag.to_sym, xml_attributes) do
+          each do |node|
+            node.append_to(builder)
+          end
+        end
+      else
+        builder.send(xml_tag.to_sym, content, xml_attributes) do
+          each do |node|
+            node.append_to(builder)
+          end
         end
       end
     end
lib/scale/text.rb
@@ -0,0 +1,16 @@
+module Scale
+  class Text
+    include Node
+    attribute :x, Integer
+    attribute :y, Integer
+
+    def initialize(text, attributes = {})
+      @content = text
+      super(attributes)
+    end
+
+    def xml_tag
+      :text_
+    end
+  end
+end
lib/scale.rb
@@ -4,6 +4,7 @@ require "scale/svg"
 require "scale/shapes/rectangle"
 require "scale/shapes/circle"
 require "scale/shapes/ellipse"
+require "scale/text"
 
 module Scale
   # Your code goes here... NOT!
spec/text_spec.rb
@@ -0,0 +1,15 @@
+describe Scale::Text do
+  subject { Scale::Text.new("Hello World!") }
+
+  describe "#to_xml" do
+    it 'generates the proper xml' do
+      subject.x = 10
+      subject.y = 10
+      expected = <<-XML
+<?xml version=\"1.0\"?>
+<text x="10" y="10">Hello World!</text>
+      XML
+      expect(subject.to_xml).to eql(expected)
+    end
+  end
+end