2006-12-23 22:32:05 +00:00
|
|
|
require 'sass/constant/literal'
|
|
|
|
|
2006-12-23 21:23:27 +00:00
|
|
|
module Sass::Constant
|
2006-12-23 22:57:31 +00:00
|
|
|
class Number < Literal
|
2006-12-23 22:32:05 +00:00
|
|
|
|
2006-12-23 22:57:31 +00:00
|
|
|
def parse(value)
|
2006-12-24 23:43:24 +00:00
|
|
|
value = value.include?('.') ? value.to_f : value.to_i
|
2006-12-23 21:23:27 +00:00
|
|
|
@value = value
|
|
|
|
end
|
|
|
|
|
2006-12-23 22:57:31 +00:00
|
|
|
def plus(other)
|
2006-12-24 00:23:28 +00:00
|
|
|
if other.is_a? Number
|
2006-12-24 23:05:07 +00:00
|
|
|
operate(other, :+)
|
|
|
|
elsif other.is_a? Color
|
|
|
|
other.plus(self)
|
2006-12-24 00:23:28 +00:00
|
|
|
else
|
|
|
|
Sass::Constant::String.from_value(self.to_s + other.to_s)
|
|
|
|
end
|
2006-12-23 22:57:31 +00:00
|
|
|
end
|
|
|
|
|
2006-12-24 23:43:24 +00:00
|
|
|
def minus(other)
|
|
|
|
if other.is_a? Number
|
|
|
|
operate(other, :-)
|
|
|
|
else
|
|
|
|
raise NoMethodError.new(nil, :minus)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-12-24 23:05:07 +00:00
|
|
|
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
|
|
|
|
|
2006-12-24 23:43:24 +00:00
|
|
|
def div(other)
|
|
|
|
if other.is_a? Number
|
|
|
|
operate(other, :/)
|
|
|
|
else
|
|
|
|
raise NoMethodError.new(nil, :div)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-12-24 23:53:38 +00:00
|
|
|
def mod(other)
|
|
|
|
if other.is_a? Number
|
|
|
|
operate(other, :%)
|
|
|
|
else
|
|
|
|
raise NoMethodError.new(nil, :mod)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-12-23 21:23:27 +00:00
|
|
|
def to_s
|
2006-12-24 23:43:24 +00:00
|
|
|
value = @value
|
|
|
|
value = value.to_i if value % 1 == 0.0
|
|
|
|
value.to_s
|
2006-12-23 21:23:27 +00:00
|
|
|
end
|
2006-12-24 23:05:07 +00:00
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def operate(other, operation)
|
|
|
|
Number.from_value(self.value.send(operation, other.value))
|
|
|
|
end
|
2006-12-23 21:23:27 +00:00
|
|
|
end
|
|
|
|
end
|