Commit 1d1d3b2
Changed files (4)
lib
scale
lib/scale/node.rb
@@ -0,0 +1,26 @@
+module Scale
+ module Node
+ def children
+ @children ||= []
+ end
+
+ def add(node)
+ children.push(node)
+ end
+
+ def to_xml
+ builder = Nokogiri::XML::Builder.new do |xml|
+ append_to(xml)
+ end
+ builder.to_xml
+ end
+
+ def append_to(xml)
+ xml.send(xml_tag.to_sym, attributes) do
+ children.each do |node|
+ node.append_to(xml)
+ end
+ end
+ end
+ end
+end
lib/scale/rectangle.rb
@@ -1,13 +1,19 @@
module Scale
class Rectangle
+ include Node
+
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)
+ def xml_tag
+ :rect
+ end
+
+ def attributes
+ { width: @width, height: @height, fill: @fill }
end
end
end
lib/scale/svg.rb
@@ -2,34 +2,19 @@ require 'nokogiri'
module Scale
class SVG
+ include Node
attr_accessor :width, :height
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|
- append_to(xml)
- end
- builder.to_xml
- end
+ private
- def append_to(xml)
- xml.svg(attributes) do
- @children.each do |node|
- node.append_to(xml)
- end
- end
+ def xml_tag
+ :svg
end
- private
-
def attributes
attributes = {
version: "1.1",
lib/scale.rb
@@ -1,4 +1,5 @@
require "scale/version"
+require "scale/node"
require "scale/svg"
require "scale/rectangle"