shuffle things a bit for orderliness.

Johnathon Wright committed Apr 19, 2011
commit 7752dd63ea147d8f701e548918d1d822c04bac7c
Showing 16 changed files with 186 additions and 158 deletions
examples/smile.rb +1 -1
@@ @@ -1,4 +1,4 @@
- class Smile < Sketch::Base
+ class Smile < Sketch
# def draw( canvas )
# circle.new(:id => 'face', :cx => 200, :cy => 200, :r => 195, :fill => "yellow")
sketch.rb b/lib/sketch.rb +49 -0
@@ @@ -0,0 +1,49 @@
+ class Sketch < Valuable
+
+ has_value :id
+ has_value :klass
+ has_value :height
+ has_value :width
+ has_value :baseProfile, :alias => :base_profile
+ has_value :doctype
+
+ def draw(canvas)
+ raise NotImplementedError
+ end
+
+ def to_svg( )
+ svg_builder do |canvas|
+ self.draw( canvas )
+ end.to_xml
+ end
+
+ def to_html
+ svg_builder do |canvas|
+ self.draw( canvas )
+ end.doc.root.to_s
+ end
+
+ def svg_attributes
+ out = {}
+ out[:height] = self.height if self.height
+ out[:width] = self.width if self.width
+ out[:doctype] = self.doctype if self.doctype
+ out
+ end
+
+ def svg_builder
+ Canvas.new( svg_attributes ).builder {|canvas| yield canvas}
+ end
+
+ end
+
+ require 'sketch/base'
+ require 'sketch/element'
+ require 'sketch/canvas'
+
+ require 'sketch/circle'
+ require 'sketch/ellipse'
+ require 'sketch/rect'
+ require 'sketch/rectangle'
+ require 'sketch/path'
+
sketch/base.rb b/lib/sketch/base.rb +8 -32
@@ @@ -1,39 +1,15 @@
- module Sketch
- class Base < Valuable
+ module Sketch::Base
+ def svg_attributes
+ out = {}
- has_value :id
- has_value :klass
-
- def to_svg( doc_atts = {} )
- svg_builder(doc_atts) do |canvas|
- self.draw( canvas )
- end.to_xml
- end
-
- def to_html( doc_atts = {} )
- svg_builder(doc_atts) do |canvas|
- self.draw( canvas )
- end.doc.root
- end
-
- def svg_attributes
- out = {}
-
- #svg is heavy in dashes. Ruby symbols can't handle them.
- attributes.each do |name, value|
- out[name.to_s.gsub('_', '-')] = value
- end
-
- out[:class] = out.delete(:klass) if out.has_key?(:klass)
-
- out
+ #svg is heavy in dashes. Ruby symbols can't handle them.
+ attributes.each do |name, value|
+ out[name.to_s.gsub('_', '-')] = value
end
- private
+ out[:class] = out.delete(:klass) if out.has_key?(:klass)
- def svg_builder(atts)
- Canvas.new( atts ).builder {|canvas| yield canvas}
- end
+ out
end
end
sketch/canvas.rb b/lib/sketch/canvas.rb +36 -36
@@ @@ -1,43 +1,43 @@
- module Sketch
- class Canvas < Valuable
-
- has_value :doctype, :default => false
- has_value :inline, :default => false
- has_value :svg_version, :default => '1.1'
- has_value :namespace_bindings, :default => [:svg, :xlink, :events]
-
- has_value :width, :default => 500
- has_value :height, :default => 300
-
- def builder
- Nokogiri::XML::Builder.new do |document|
- render_doctype( document )
- document.svg( svg_attributes ) {|svg| yield svg if block_given?}
- end
- end
+ class Sketch::Canvas < Valuable
+ include Sketch::Base
+
+ has_value :version, :default => '1.1'
+ has_value :height
+ has_value :width
+ has_value :doctype, :default => false
- def default_doctype
- ['svg', "-//W3C//DTD SVG 1.0//EN", "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"]
+ def namespace_bindings
+ {
+ 'xmlns' => 'http://www.w3.org/2000/svg',
+ 'xmlns:ev' => 'http://www.w3.org/2001/xml-events',
+ 'xmlns:xlink' => 'http://www.w3.org/1999/xlink'
+ }
+ end
+
+ def builder
+ Nokogiri::XML::Builder.new do |document|
+ render_doctype( document )
+ document.svg( svg_attributes ) {|svg| yield svg if block_given?}
end
+ end
+
+ def default_doctype
+ ['svg', "-//W3C//DTD SVG 1.0//EN", "http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd"]
+ end
- def render_doctype( document )
- self.doctype &&= self.default_doctype
+ def render_doctype( document )
+ self.doctype &&= self.default_doctype
- if( self.doctype )
- document.doc.create_internal_subset(*self.doctype)
- end
- end
-
- def svg_attributes
- {
- 'width' => width,
- 'height' => height,
- 'version' => svg_version,
- 'xmlns' => 'http://www.w3.org/2000/svg',
- 'xmlns:ev' => 'http://www.w3.org/2001/xml-events',
- 'xmlns:xlink' => 'http://www.w3.org/1999/xlink',
- }
+ if( self.doctype )
+ document.doc.create_internal_subset(*self.doctype)
end
-
end
+
+ def svg_attributes
+ atts = super
+ atts.delete('doctype')
+ atts.merge(namespace_bindings)
+ end
+
end
+
sketch/circle.rb b/lib/sketch/circle.rb +10 -12
@@ @@ -1,16 +1,14 @@
- module Sketch
- class Circle < Base
+ class Sketch::Circle < Sketch::Element
- has_value :cx
- has_value :cy
- has_value :r, :alias => :radius
- has_value :fill
- has_value :stroke
- has_value :stroke_width
-
- def draw(canvas)
- canvas.circle(:cx => self.cx, :cy => self.cy, :r => self.radius, :fill => self.fill, :stroke => self.stroke, 'stroke_width' => self.stroke_width)
- end
+ has_value :cx
+ has_value :cy
+ has_value :r, :alias => :radius
+ has_value :fill
+ has_value :stroke
+ has_value :stroke_width
+
+ def draw(canvas)
+ canvas.circle(:cx => self.cx, :cy => self.cy, :r => self.radius, :fill => self.fill, :stroke => self.stroke, 'stroke_width' => self.stroke_width)
end
end
sketch/element.rb b/lib/sketch/element.rb +16 -0
@@ @@ -0,0 +1,16 @@
+ class Sketch::Element < Valuable
+ include Sketch::Base
+
+ has_value :id
+ has_value :klass
+ has_value :style
+
+ def draw(canvas)
+ canvas.send( self.svg_node, svg_attributes )
+ end
+
+ def svg_node
+ self.class.name.split('::').last.downcase
+ end
+ end
+
sketch/ellipse.rb b/lib/sketch/ellipse.rb +10 -16
@@ @@ -1,19 +1,13 @@
- module Sketch
- class Ellipse < Base
+ class Sketch::Ellipse < Sketch::Element
- has_value :cx
- has_value :cy
- has_value :rx
- has_value :ry
- has_value :radius
- has_value :fill
- has_value :stroke
- has_value :stroke_width
-
- def draw(canvas)
- canvas.ellipse( svg_attributes )
- end
-
- end
+ has_value :cx
+ has_value :cy
+ has_value :rx
+ has_value :ry
+ has_value :radius
+ has_value :fill
+ has_value :stroke
+ has_value :stroke_width
+
end
sketch/path.rb b/lib/sketch/path.rb +12 -15
@@ @@ -1,20 +1,17 @@
- module Sketch
- class Path < Base
+ class Sketch::Path < Sketch::Element
- has_value :fill, :default => 'none'
- has_value :stroke, :default => 'black'
- has_value :stroke_width, :default => 1
- has_value :stroke_linecap
- has_value :d, :alias => 'commands', :default => []
+ has_value :fill
+ has_value :stroke
+ has_value :stroke_width
+ has_value :stroke_linecap
+ has_value :d, :alias => 'commands', :default => []
- def draw(canvas)
- canvas.path(svg_attributes)
- end
-
- def svg_attributes
- super.merge(:d => self.commands.join(' '))
- end
-
+ def draw(canvas)
+ canvas.path(svg_attributes)
+ end
+
+ def svg_attributes
+ super.merge(:d => self.commands.join(' '))
end
end
sketch/rect.rb b/lib/sketch/rect.rb +14 -0
@@ @@ -0,0 +1,14 @@
+ class Sketch::Rect < Sketch::Element
+
+ has_value :x
+ has_value :y
+ has_value :width
+ has_value :height
+ has_value :rx
+ has_value :ry
+ has_value :fill
+ has_value :stroke
+ has_value :stroke_width
+
+ end
+
sketch/rectangle.rb b/lib/sketch/rectangle.rb +1 -0
@@ @@ -0,0 +1 @@
+ Sketch::Rectangle = Sketch::Rect
test/.canvas_test.rb.swp +0 -0
test/base_test.rb +8 -17
@@ @@ -1,24 +1,15 @@
- #prerequisites
- require 'rubygems'
- require 'test/unit'
- require 'nokogiri'
- require 'valuable'
+ require 'test/helper.rb'
- # require file being tested
- $: << File.expand_path(File.dirname(__FILE__) + '/../lib/sketch')
- require 'base.rb'
- require 'canvas.rb'
-
- class EmptySquare < Sketch::Base
- def draw(canvas)
- canvas.rect('x' => 50, 'y' => 20, 'width' => 10, 'height' => 20)
- end
+ class Squigly < Sketch::Element
+ has_value :reflective_coating
end
- class BaseTest < Test::Unit::TestCase
+ class SketchTest < Test::Unit::TestCase
- def test_that_nodes_are_added_to_the_canvas
- assert_equal 1, Nokogiri::XML(EmptySquare.new.to_svg).css('rect[x="50"][y="20"][width="10"][height="20"]').size
+ def test_that_node_is_based_on_name
+ canvas = stub
+ canvas.expects(:squigly)
+ Squigly.new.draw(canvas)
end
end
test/canvas_test.rb +1 -9
@@ @@ -1,12 +1,4 @@
- #prerequisites
- require 'rubygems'
- require 'test/unit'
- require 'nokogiri'
- require 'valuable'
-
- # require file being tested
- $: << File.expand_path(File.dirname(__FILE__) + '/../lib/sketch')
- require 'canvas.rb'
+ require 'helper'
class CanvasTest < Test::Unit::TestCase
test/doctype_test.rb +3 -11
@@ @@ -1,14 +1,6 @@
- #prerequisites
- require 'rubygems'
- require 'nokogiri'
- require 'valuable'
+ require 'helper'
- # require file being tested
- $: << File.expand_path(File.dirname(__FILE__) + '/../lib/sketch')
- require 'canvas.rb'
- require 'base.rb'
-
- class Minimalist < Sketch::Base
+ class Minimalist < Sketch
def draw( canvas )
end
end
@@ @@ -26,7 +18,7 @@ class DoctypeTest < Test::Unit::TestCase
end
def test_that_it_includes_doctype_when_configured
- artwork = Minimalist.new.to_svg(:doctype => true)
+ artwork = Minimalist.new(:doctype => true).to_svg
assert_match(/doctype/i, artwork)
end
test/helper.rb +15 -0
@@ @@ -0,0 +1,15 @@
+ #prerequisites
+ require 'rubygems'
+ require 'test/unit'
+ require 'mocha'
+ require 'nokogiri'
+ require 'valuable'
+
+ $:.push File.expand_path("./../../lib", __FILE__)
+ $:.push File.expand_path("./../../lib/sketch", __FILE__)
+
+ require 'sketch'
+ require 'sketch/canvas'
+ require 'sketch/base'
+ require 'sketch/element'
+
test/svg_namespace_test.rb +2 -9
@@ @@ -1,13 +1,6 @@
- #prerequisites
- require 'nokogiri'
- require 'valuable'
+ require 'helper'
- # require file being tested
- $: << File.expand_path(File.dirname(__FILE__) + '/../lib/sketch')
- require 'canvas.rb'
- require 'base.rb'
-
- class Minimalist < Sketch::Base
+ class Minimalist < Sketch
def draw( canvas )
end
end