1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

[Sass] Make sure the default string ops apply to all types.

This commit is contained in:
Nathan Weizenbaum 2009-03-24 01:31:37 -07:00
parent b2b91ed30f
commit 470c54dcee
5 changed files with 37 additions and 45 deletions

View file

@ -31,42 +31,42 @@ module Sass::Script
end
def plus(other)
if other.is_a? Sass::Script::String
Sass::Script::String.new(self.to_s + other.to_s)
else
if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
piecewise(other, :+)
else
super
end
end
def minus(other)
if other.is_a? Sass::Script::String
raise NoMethodError.new(nil, :minus)
else
if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
piecewise(other, :-)
else
super
end
end
def times(other)
if other.is_a? Sass::Script::String
raise NoMethodError.new(nil, :times)
else
if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
piecewise(other, :*)
else
raise NoMethodError.new(nil, :times)
end
end
def div(other)
if other.is_a? Sass::Script::String
raise NoMethodError.new(nil, :div)
else
if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
piecewise(other, :/)
else
super
end
end
def mod(other)
if other.is_a? Sass::Script::String
raise NoMethodError.new(nil, :mod)
else
if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
piecewise(other, :%)
else
raise NoMethodError.new(nil, :mod)
end
end

View file

@ -42,6 +42,26 @@ class Sass::Script::Literal # :nodoc:
Sass::Script::String.new("#{self.to_s}, #{other.to_s}")
end
def plus(other)
Sass::Script::String.new(self.to_s + other.to_s)
end
def minus(other)
Sass::Script::String.new("#{self.to_s}-#{other.to_s}")
end
def div(other)
Sass::Script::String.new("#{self.to_s}/#{other.to_s}")
end
def unary_minus
Sass::Script::String.new("-#{self.to_s}")
end
def unary_div
Sass::Script::String.new("/#{self.to_s}")
end
def inspect
value.inspect
end

View file

@ -28,7 +28,7 @@ module Sass::Script
if other.is_a? Number
operate(other, :-)
else
raise NoMethodError.new(nil, :minus)
super
end
end
@ -50,7 +50,7 @@ module Sass::Script
if other.is_a? Number
operate(other, :/)
else
raise NoMethodError.new(nil, :div)
super
end
end

View file

@ -11,26 +11,6 @@ module Sass::Script
Sass::Script::String.new(interpolated)
end
def plus(other)
Sass::Script::String.new(self.to_s + other.to_s)
end
def minus(other)
Sass::Script::String.new("#{self.to_s}-#{other.to_s}")
end
def unary_minus
Sass::Script::String.new("-#{self.to_s}")
end
def div(other)
Sass::Script::String.new("#{self.to_s}/#{other.to_s}")
end
def unary_div
Sass::Script::String.new("/#{self.to_s}")
end
def to_s
@value
end

View file

@ -15,14 +15,6 @@ class SassEngineTest < Test::Unit::TestCase
"!a = foo(\"bar\"" => 'Expected rparen token, was end of text.',
"!a = 1 }" => 'Unexpected end_interpolation token.',
"!a = 1 }foo\"" => 'Unexpected end_interpolation token.',
"!a = #aaa - \"a\"" => 'Undefined operation: "#aaaaaa minus a".',
"!a = #aaa / \"a\"" => 'Undefined operation: "#aaaaaa div a".',
"!a = #aaa * \"a\"" => 'Undefined operation: "#aaaaaa times a".',
"!a = #aaa % \"a\"" => 'Undefined operation: "#aaaaaa mod a".',
"!a = 1 - \"a\"" => 'Undefined operation: "1 minus a".',
"!a = 1 * \"a\"" => 'Undefined operation: "1 times a".',
"!a = 1 / \"a\"" => 'Undefined operation: "1 div a".',
"!a = 1 % \"a\"" => 'Undefined operation: "1 mod a".',
":" => 'Invalid attribute: ":".',
": a" => 'Invalid attribute: ": a".',
":= a" => 'Invalid attribute: ":= a".',