actually, several elements have central points.

Johnathon E. Wright committed Nov 10, 2011
commit 260d7124523326a770ca022f647074642858df96
Showing 6 changed files with 33 additions and 19 deletions
sketch/circle.rb b/lib/sketch/circle.rb +1 -7
@@ @@ -8,13 +8,7 @@ class Sketch::Circle < Sketch::Element
has_value :stroke
has_value :stroke_width
- def point=(coordinates)
- self.cx, self.cy = *coordinates
- end
-
- def point
- [self.cx, self.cy]
- end
+ central_point :cx, :cy
end
sketch/element.rb b/lib/sketch/element.rb +12 -0
@@ @@ -13,5 +13,17 @@ class Sketch::Element < Valuable
def svg_node
self.class.name.split('::').last.downcase
end
+
+ class << self
+ def central_point( x_param = :x, y_param = :y )
+ define_method :point= do |coordinates|
+ attributes[x_param], attributes[y_param] = *coordinates
+ end
+
+ define_method :point do
+ [attributes[x_param], attributes[y_param]]
+ end
+ end
+ end
end
sketch/ellipse.rb b/lib/sketch/ellipse.rb +2 -0
@@ @@ -9,5 +9,7 @@ class Sketch::Ellipse < Sketch::Element
has_value :stroke
has_value :stroke_width
+ central_point :cx, :cy
+
end
sketch/rect.rb b/lib/sketch/rect.rb +2 -0
@@ @@ -10,5 +10,7 @@ class Sketch::Rect < Sketch::Element
has_value :stroke
has_value :stroke_width
+ central_point :x, :y
+
end
test/circle_test.rb +0 -12
@@ @@ -1,12 +0,0 @@
- require 'helper'
-
- class CircleTest < Test::Unit::TestCase
-
- def test_that_point_is_broken_into_coordinates
- c = Sketch::Circle.new(:point => [10, 20])
- assert_equal 10, c.cx
- assert_equal 20, c.cy
- end
-
- end
-
test/element_test.rb +16 -0
@@ @@ -0,0 +1,16 @@
+ require 'helper'
+
+ class ElementTest < Test::Unit::TestCase
+
+ def test_that_point_is_broken_into_coordinates
+ c = Sketch::Circle.new(:point => [10, 20])
+ assert_equal 10, c.cx
+ assert_equal 20, c.cy
+ end
+
+ def test_that_coordinates_are_available_as_point
+ r = Sketch::Rectangle.new(:x => 33, :y => 55)
+ assert_equal [33, 55], r.point
+ end
+ end
+