1
0
Fork 0
mirror of https://github.com/haml/haml.git synced 2022-11-09 12:33:31 -05:00

More operator/constant structure changes, plus operator defined for numbers.

git-svn-id: svn://hamptoncatlin.com/haml/trunk@257 7063305b-7217-0410-af8c-cdc13e5119b9
This commit is contained in:
nex3 2006-12-23 22:57:31 +00:00
parent 4246a12203
commit 99ee33a7bd
7 changed files with 35 additions and 17 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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; }

View file

@ -1,4 +1,4 @@
!width = 500px
!width = 10 + 20
!color = #00ff98
!main_text = #ffa
!num = 10