Skip to content

Commit

Permalink
Qcontent::Dimension now includes the ability to 'name' dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
quirkey committed Jun 28, 2009
1 parent 46f0891 commit 52d58f6
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 5 deletions.
20 changes: 15 additions & 5 deletions lib/qcontent/dimension.rb
Expand Up @@ -3,21 +3,31 @@ class Dimension
class InvalidDimension < ::RuntimeError; end;
include Comparable

attr_accessor :width, :height
attr_accessor :name, :width, :height

def initialize(*args)
first = args.shift
case first
when Array
self.width, self.height = first
when Hash
self.width = first['width'] || first[:width]
self.width = first['width'] || first[:width]
self.height = first['height'] || first[:height]
self.name = first['name'] || first[:name]
when Dimension
return first
when Fixnum
self.width = first
self.height = args.shift
else
self.width, self.height = (first.is_a?(String) ? first.split('x') : first)
self.height ||= args.shift
if first.to_i == 0 # its a name
self.name = first
d = Dimension.new(*args)
self.width, self.height = d.width, d.height
else
self.width, self.height = (first.is_a?(String) ? first.split('x') : first)
self.height ||= args.shift
end
end
end

Expand All @@ -34,7 +44,7 @@ def height=(h)
end

def to_s(join = 'x')
"#{width}#{join}#{height}"
name ? name : "#{width}#{join}#{height}"
end

def to_a
Expand Down
95 changes: 95 additions & 0 deletions test/test_dimension.rb
Expand Up @@ -88,13 +88,108 @@ class TestDimension < Test::Unit::TestCase
end
end

context "with 3 strings" do
setup do
@dimension = Qcontent::Dimension.new('medium','400','300')
end

should "set width" do
assert_equal 400, @dimension.width
end

should "set height" do
assert_equal 300, @dimension.height
end

should "set name" do
assert_equal 'medium', @dimension.name
end
end

context "with a string and an array" do
setup do
@dimension = Qcontent::Dimension.new('medium',['400','300'])
end

should "set width" do
assert_equal 400, @dimension.width
end

should "set height" do
assert_equal 300, @dimension.height
end

should "set name" do
assert_equal 'medium', @dimension.name
end
end

context "with a hash with a name" do
setup do
@dimension = Qcontent::Dimension.new({:width => 400, :height => 300, :name => 'medium'})
end

should "set width" do
assert_equal 400, @dimension.width
end

should "set height" do
assert_equal 300, @dimension.height
end

should "set name" do
assert_equal 'medium', @dimension.name
end
end

context "with two strings one with a name" do
setup do
@dimension = Qcontent::Dimension.new('medium','400x300')
end

should "set width" do
assert_equal 400, @dimension.width
end

should "set height" do
assert_equal 300, @dimension.height
end

should "set name" do
assert_equal 'medium', @dimension.name
end
end

context "with a string and two integers" do
setup do
@dimension = Qcontent::Dimension.new('medium',400,300)
end

should "set width" do
assert_equal 400, @dimension.width
end

should "set height" do
assert_equal 300, @dimension.height
end

should "set name" do
assert_equal 'medium', @dimension.name
end
end

end

context "#to_s" do
should "join with and height" do
@dimension = Qcontent::Dimension.new(['400','300'])
assert_equal '400x300', @dimension.to_s
end

should "return name if it exists" do
@dimension = Qcontent::Dimension.new('medium', ['400','300'])
assert_equal 'medium', @dimension.to_s
end
end

context "#to_a" do
Expand Down

0 comments on commit 52d58f6

Please sign in to comment.