2006-12-23 22:32:05 +00:00
|
|
|
require 'sass/constant/literal'
|
|
|
|
|
2007-03-01 05:52:47 +00:00
|
|
|
module Sass::Constant # :nodoc:
|
|
|
|
class Number < Literal # :nodoc:
|
2007-02-10 06:38:50 +00:00
|
|
|
|
|
|
|
attr_reader :unit
|
|
|
|
|
2006-12-23 22:57:31 +00:00
|
|
|
def parse(value)
|
2007-02-10 06:38:50 +00:00
|
|
|
first, second, unit = value.scan(Literal::NUMBER)[0]
|
|
|
|
@value = first.empty? ? second.to_i : "#{first}#{second}".to_f
|
|
|
|
@unit = unit unless unit.empty?
|
2006-12-23 21:23:27 +00:00
|
|
|
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
|
2007-02-10 06:38:50 +00:00
|
|
|
"#{value}#{@unit}"
|
2006-12-23 21:23:27 +00:00
|
|
|
end
|
2006-12-24 23:05:07 +00:00
|
|
|
|
|
|
|
protected
|
2007-02-10 06:38:50 +00:00
|
|
|
|
|
|
|
def self.from_value(value, unit=nil)
|
|
|
|
instance = super(value)
|
|
|
|
instance.instance_variable_set('@unit', unit)
|
|
|
|
instance
|
|
|
|
end
|
2006-12-24 23:05:07 +00:00
|
|
|
|
|
|
|
def operate(other, operation)
|
2007-02-10 06:38:50 +00:00
|
|
|
unit = nil
|
|
|
|
if other.unit.nil?
|
|
|
|
unit = self.unit
|
|
|
|
elsif self.unit.nil?
|
|
|
|
unit = other.unit
|
|
|
|
elsif other.unit == self.unit
|
|
|
|
unit = self.unit
|
|
|
|
else
|
|
|
|
raise Sass::SyntaxError.new("Incompatible units: #{self.unit} and #{other.unit}")
|
|
|
|
end
|
|
|
|
|
|
|
|
Number.from_value(self.value.send(operation, other.value), unit)
|
2006-12-24 23:05:07 +00:00
|
|
|
end
|
2006-12-23 21:23:27 +00:00
|
|
|
end
|
|
|
|
end
|