Permalink
Browse files

Adding accessors and validation for size

  • Loading branch information...
deepakjois committed May 25, 2008
1 parent 7f81e62 commit 5e6cd1ea4cea52f1bfef2510fbf158683d58d37d
Showing with 48 additions and 1 deletion.
  1. +32 −1 lib/google_chart/base.rb
  2. +16 −0 spec/gchartrb_spec.rb
View
@@ -50,11 +50,42 @@ def title=(title) #:nodoc:
@title = title.gsub("\n", "|")
end
- # Size in WIDTHxHEIGHT format
+ # Sets the chart's width, in pixels. Raises +ArgumentError+
+ # if +width+ is less than 1 or greater than 1,000.
+ def width=(width)
+ if width.nil? || width < 1 || width > 1_000
+ raise ArgumentError, "Invalid width: #{width.inspect}"
+ end
+
+ @width = width
+ end
+
+ # Sets the chart's height, in pixels. Raises +ArgumentError+
+ # if +height+ is less than 1 or greater than 1,000.
+ def height=(height)
+ if height.nil? || height < 1 || height > 1_000
+ raise ArgumentError, "Invalid height: #{height.inspect}"
+ end
+
+ @height = height
+ end
+
+ # Returns the chart's size as "WIDTHxHEIGHT".
def size
"#{width}x#{height}"
end
+ # Sets the chart's size as "WIDTHxHEIGHT". Raises +ArgumentError+
+ # if +width+ * +height+ is greater than 300,000 pixels.
+ def size=(size)
+ self.width, self.height = size.split("x").collect { |n| Integer(n) }
+
+ if (width * height) > 300_000
+ raise ArgumentError, "Invalid size: #{size.inspect} yields a graph with more than 300,000 pixels"
+ end
+ end
+
+
# Set the data encoding to one of :simple, :text or :extended
def encoding=(enc)
raise ArgumentError.new("unsupported encoding: #{encoding.inspect}") unless GoogleChart::ENCODINGS.include?(enc)
View
@@ -28,6 +28,22 @@ class <<self ; public :new ; end
@dummy.height.should == 200
end
+ it "should able to initialise size via a WIDTHxHEIGHT string" do
+ @dummy.size.should == "320x200"
+ @dummy.size = "400x500"
+ @dummy.width.should == 400
+ @dummy.height.should == 500
+ end
+
+ it "should raise an error if the height or width is greater than 1000" do
+ lambda { @dummy.height = 1001 }.should raise_error(ArgumentError)
+ lambda { @dummy.width = 1001 }.should raise_error(ArgumentError)
+ end
+
+ it "should raise error if width x height is greater than 300000" do
+ lambda { @dummy.size = "400x1000" }.should raise_error(ArgumentError)
+ end
+
it "should have a default encoding of :simple" do
@dummy.encoding.should == :simple
end

0 comments on commit 5e6cd1e

Please sign in to comment.