1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00
haml--haml/lib/sass/constant/number.rb
nex3 8895413d4e Modulo, making constant script pretty much complete.
git-svn-id: svn://hamptoncatlin.com/haml/trunk@264 7063305b-7217-0410-af8c-cdc13e5119b9
2006-12-24 23:53:38 +00:00

67 lines
1.3 KiB
Ruby

require 'sass/constant/literal'
module Sass::Constant
class Number < Literal
def parse(value)
value = value.include?('.') ? value.to_f : value.to_i
@value = value
end
def plus(other)
if other.is_a? Number
operate(other, :+)
elsif other.is_a? Color
other.plus(self)
else
Sass::Constant::String.from_value(self.to_s + other.to_s)
end
end
def minus(other)
if other.is_a? Number
operate(other, :-)
else
raise NoMethodError.new(nil, :minus)
end
end
def times(other)
if other.is_a? Number
operate(other, :*)
elsif other.is_a? Color
other.times(self)
else
raise NoMethodError.new(nil, :times)
end
end
def div(other)
if other.is_a? Number
operate(other, :/)
else
raise NoMethodError.new(nil, :div)
end
end
def mod(other)
if other.is_a? Number
operate(other, :%)
else
raise NoMethodError.new(nil, :mod)
end
end
def to_s
value = @value
value = value.to_i if value % 1 == 0.0
value.to_s
end
protected
def operate(other, operation)
Number.from_value(self.value.send(operation, other.value))
end
end
end