Sha256: 9258e8ee3bb21601b46e6ad871596fdf120d58c0fbce5cf843bc09647059d7df

Contents?: true

Size: 1.85 KB

Versions: 9

Compression:

Stored size: 1.85 KB

Contents

require 'sass/constant/literal'

module Sass::Constant  # :nodoc:
  class Number < Literal # :nodoc:

    attr_reader :unit

    def parse(value)
      first, second, unit = value.scan(Literal::NUMBER)[0]
      @value = first.empty? ? second.to_i : "#{first}#{second}".to_f
      @unit = unit unless unit.empty?
    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}#{@unit}"
    end

    protected

    def self.from_value(value, unit=nil)
      instance = super(value)
      instance.instance_variable_set('@unit', unit)
      instance
    end

    def operate(other, operation)
      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)
    end
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
radiant-0.7.2 vendor/plugins/haml/lib/sass/constant/number.rb
haml-2.0.0 lib/sass/constant/number.rb
haml-2.0.2 lib/sass/constant/number.rb
haml-2.0.1 lib/sass/constant/number.rb
radiant-0.6.7 vendor/plugins/haml/lib/sass/constant/number.rb
radiant-0.6.9 vendor/plugins/haml/lib/sass/constant/number.rb
radiant-0.6.8 vendor/plugins/haml/lib/sass/constant/number.rb
radiant-0.7.0 vendor/plugins/haml/lib/sass/constant/number.rb
radiant-0.7.1 vendor/plugins/haml/lib/sass/constant/number.rb