diff --git a/lib/sass/constant/color.rb b/lib/sass/constant/color.rb index 65fe58d6..f827702c 100644 --- a/lib/sass/constant/color.rb +++ b/lib/sass/constant/color.rb @@ -1,17 +1,16 @@ require 'sass/constant/literal' module Sass::Constant - class Color - include Literal + class Color < Literal REGEXP = /\##{"([0-9a-f]{1,2})" * 3}/ - def initialize(value) - @red, @green, @blue = value.scan(REGEXP)[0].map { |num| num.ljust(2, 'f').to_i(16) } + def parse(value) + @value = value.scan(REGEXP)[0].map { |num| num.ljust(2, 'f').to_i(16) } end def to_s - red, green, blue = [@red, @green, @blue].map { |num| num.to_s(16).rjust(2, '0') } + red, green, blue = @value.map { |num| num.to_s(16).rjust(2, '0') } "##{red}#{green}#{blue}" end end diff --git a/lib/sass/constant/literal.rb b/lib/sass/constant/literal.rb index 2fc52f50..46a735a1 100644 --- a/lib/sass/constant/literal.rb +++ b/lib/sass/constant/literal.rb @@ -1,10 +1,10 @@ -module Sass::Constant; module Literal; end; end; # Let the subclasses see the superclass +module Sass::Constant; class Literal; end; end; # Let the subclasses see the superclass require 'sass/constant/string' require 'sass/constant/number' require 'sass/constant/color' -module Sass::Constant::Literal +class Sass::Constant::Literal # The regular expression matching numbers. NUMBER = /^[0-9]*\.?[0-9]+$/ @@ -22,7 +22,20 @@ module Sass::Constant::Literal end end + def initialize(value = nil) + self.parse(value) if value + end + def perform self end + + protected + + attr_reader :value + + def self.from_value(value) + instance = self.new + instance.instance_variable_set('@value', value) + end end diff --git a/lib/sass/constant/number.rb b/lib/sass/constant/number.rb index aa8bb275..089fbe3e 100644 --- a/lib/sass/constant/number.rb +++ b/lib/sass/constant/number.rb @@ -1,15 +1,18 @@ require 'sass/constant/literal' module Sass::Constant - class Number - include Literal + class Number < Literal - def initialize(value) + def parse(value) value = value.to_f value = value.to_i if value % 1 == 0.0 @value = value end + def plus(other) + Number.from_value(self.value + other.value) + end + def to_s @value.to_s end diff --git a/lib/sass/constant/operation.rb b/lib/sass/constant/operation.rb index 87b843b9..c89575a5 100644 --- a/lib/sass/constant/operation.rb +++ b/lib/sass/constant/operation.rb @@ -21,10 +21,14 @@ module Sass::Constant literal2 = @operand2.perform begin literal1.send(@operator, literal2) - rescue NoMethodError + rescue NoMethodError => e + raise e unless e.name == @operator + begin literal2.send(@operator, literal1) - rescue NoMethodError + rescue NoMethodError => e + raise e unless e.name == @operator + raise "Undefined operation:\n#{literal1} #{@operator} #{literal2}\n" end end diff --git a/lib/sass/constant/string.rb b/lib/sass/constant/string.rb index 88876e09..141b2b17 100644 --- a/lib/sass/constant/string.rb +++ b/lib/sass/constant/string.rb @@ -1,10 +1,9 @@ require 'sass/constant/literal' module Sass::Constant - class String - include Literal + class String < Literal - def initialize(value) + def parse(value) @value = value end diff --git a/test/sass/results/constants.css b/test/sass/results/constants.css index 2a2e8719..35b7592f 100644 --- a/test/sass/results/constants.css +++ b/test/sass/results/constants.css @@ -1,2 +1,2 @@ -#main { width: 500px; background-color: #000; color: #ffffaf; } +#main { width: 30; background-color: #000; color: #ffffaf; } #main #sidebar { background-color: #00ff98; num-normal: 10; num-dec: 10.2; num-dec0: 99; num-neg: -10; esc: 10+12; } diff --git a/test/sass/templates/constants.sass b/test/sass/templates/constants.sass index c383cd8c..9ea61754 100644 --- a/test/sass/templates/constants.sass +++ b/test/sass/templates/constants.sass @@ -1,4 +1,4 @@ -!width = 500px +!width = 10 + 20 !color = #00ff98 !main_text = #ffa !num = 10