Class: Sass::Script::Number
- Defined in:
- lib/sass/script/number.rb
Overview
A SassScript object representing a number.
SassScript numbers can have decimal values,
and can also have units.
For example, 12
, 1px
, and 10.45em
are all valid values.
Numbers can also have more complex units, such as 1px*em/in
.
These cannot be inputted directly in Sass code at the moment.
Constant Summary collapse
- PRECISION =
The precision with which numbers will be printed to CSS files. For example, if this is
1000.0
,3.1415926
will be printed as3.142
. 1000.0
- CONVERTABLE_UNITS =
A hash of unit names to their index in the conversion table
{"in" => 0, "cm" => 1, "pc" => 2, "mm" => 3, "pt" => 4}
- CONVERSION_TABLE =
in
[[ 1, 2.54, 6, 25.4, 72 ], # in [ nil, 1, 2.36220473, 10, 28.3464567], # cm [ nil, nil, 1, 4.23333333, 12 ], # pc [ nil, nil, nil, 1, 2.83464567], # mm [ nil, nil, nil, nil, 1 ]]
Instance Attribute Summary collapse
-
#denominator_units ⇒ Array<String>
readonly
A list of units in the denominator of the number.
-
#numerator_units ⇒ Array<String>
readonly
A list of units in the numerator of the number.
-
#value ⇒ Numeric
readonly
The Ruby value of the number.
Instance Method Summary collapse
-
#coerce(num_units, den_units) ⇒ Number
Returns this number converted to other units.
- #coercion_factor(from_units, to_units) ⇒ Object protected
- #compute_units(this, other, operation) ⇒ Object protected
- #conversion_factor(from_unit, to_unit) ⇒ Object protected
- #convertable?(units) ⇒ Boolean protected
-
#div(other) ⇒ Literal
The SassScript
/
operation. -
#eq(other) ⇒ Boolean
The SassScript
==
operation. -
#gt(other) ⇒ Boolean
The SassScript
>
operation. -
#gte(other) ⇒ Boolean
The SassScript
>=
operation. -
#initialize(value, numerator_units = [], denominator_units = []) ⇒ Number
constructor
A new instance of Number.
-
#inspect ⇒ String
Returns a readable representation of this number.
-
#int? ⇒ Boolean
Whether or not this number is an integer.
-
#legal_units? ⇒ Boolean
Whether or not this number has units that can be represented in CSS (that is, zero or one #numerator_units).
-
#lt(other) ⇒ Boolean
The SassScript
<
operation. -
#lte(other) ⇒ Boolean
The SassScript
<=
operation. -
#minus(other) ⇒ Literal
The SassScript binary
-
operation (e.g.!a - !b
). -
#mod(other) ⇒ Number
The SassScript
%
operation. - #normalize! ⇒ Object protected
- #operate(other, operation) ⇒ Object protected
-
#plus(other) ⇒ Literal
The SassScript
+
operation. - #sans_common_units(units1, units2) ⇒ Object protected
-
#times(other) ⇒ Number, Color
The SassScript
*
operation. -
#to_i ⇒ Fixnum
The integer value of the number.
-
#to_s ⇒ String
The CSS representation of this number.
-
#unary_minus ⇒ Number
The SassScript unary
-
operation (e.g.-!a
). - #unit_str ⇒ Object protected
-
#unitless? ⇒ Boolean
Whether or not this number has no units.
Methods inherited from Literal
#==, #and, #assert_int!, #comma, #concat, #neq, #or, #perform, #to_bool, #unary_div, #unary_not
Methods inherited from Node
Constructor Details
#initialize(value, numerator_units = [], denominator_units = []) ⇒ Number
Returns a new instance of Number.
36 37 38 39 40 41 |
# File 'lib/sass/script/number.rb', line 36
def initialize(value, numerator_units = [], denominator_units = [])
super(value)
@numerator_units = numerator_units
@denominator_units = denominator_units
normalize!
end
|
Instance Attribute Details
#denominator_units ⇒ Array<String> (readonly)
A list of units in the denominator of the number.
For example, 1px*em/in*cm
would return ["in", "cm"]
26 27 28 |
# File 'lib/sass/script/number.rb', line 26
def denominator_units
@denominator_units
end
|
#numerator_units ⇒ Array<String> (readonly)
A list of units in the numerator of the number.
For example, 1px*em/in*cm
would return ["px", "em"]
21 22 23 |
# File 'lib/sass/script/number.rb', line 21
def numerator_units
@numerator_units
end
|
#value ⇒ Numeric (readonly)
The Ruby value of the number.
16 17 18 |
# File 'lib/sass/script/number.rb', line 16
def value
@value
end
|
Instance Method Details
#coerce(num_units, den_units) ⇒ Number
Returns this number converted to other units. The conversion takes into account the relationship between e.g. mm and cm, as well as between e.g. in and cm.
If this number has no units, it will simply return itself with the given units.
An incompatible coercion, e.g. between px and cm, will raise an error.
278 279 280 281 282 283 284 285 |
# File 'lib/sass/script/number.rb', line 278
def coerce(num_units, den_units)
Number.new(if unitless?
self.value
else
self.value * coercion_factor(self.numerator_units, num_units) /
coercion_factor(self.denominator_units, den_units)
end, num_units, den_units)
end
|
#coercion_factor(from_units, to_units) ⇒ Object (protected)
309 310 311 312 313 314 315 316 317 318 |
# File 'lib/sass/script/number.rb', line 309
def coercion_factor(from_units, to_units)
# get a list of unmatched units
from_units, to_units = sans_common_units(from_units, to_units)
if from_units.size != to_units.size || !convertable?(from_units | to_units)
raise Sass::UnitConversionError.new("Incompatible units: '#{from_units.join('*')}' and '#{to_units.join('*')}'.")
end
from_units.zip(to_units).inject(1) {|m,p| m * conversion_factor(p[0], p[1]) }
end
|
#compute_units(this, other, operation) ⇒ Object (protected)
320 321 322 323 324 325 326 327 328 329 |
# File 'lib/sass/script/number.rb', line 320
def compute_units(this, other, operation)
case operation
when :*
[this.numerator_units + other.numerator_units, this.denominator_units + other.denominator_units]
when :/
[this.numerator_units + other.denominator_units, this.denominator_units + other.numerator_units]
else
[this.numerator_units, this.denominator_units]
end
end
|
#conversion_factor(from_unit, to_unit) ⇒ Object (protected)
361 362 363 364 365 |
# File 'lib/sass/script/number.rb', line 361
def conversion_factor(from_unit, to_unit)
res = CONVERSION_TABLE[CONVERTABLE_UNITS[from_unit]][CONVERTABLE_UNITS[to_unit]]
return 1.0 / conversion_factor(to_unit, from_unit) if res.nil?
res
end
|
#convertable?(units) ⇒ Boolean (protected)
367 368 369 |
# File 'lib/sass/script/number.rb', line 367
def convertable?(units)
Array(units).all?(&CONVERTABLE_UNITS.method(:include?))
end
|
#div(other) ⇒ Literal
The SassScript /
operation.
Its functionality depends on the type of its argument:
Sass::Script::Number : Divides this number by the other, converting units appropriately.
Literal : See Literal#div.
128 129 130 131 132 133 134 |
# File 'lib/sass/script/number.rb', line 128
def div(other)
if other.is_a? Number
operate(other, :/)
else
super
end
end
|
#eq(other) ⇒ Boolean
The SassScript ==
operation.
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/sass/script/number.rb', line 157
def eq(other)
return Sass::Script::Bool.new(false) unless other.is_a?(Sass::Script::Number)
this = self
begin
if unitless?
this = this.coerce(other.numerator_units, other.denominator_units)
else
other = other.coerce(numerator_units, denominator_units)
end
rescue Sass::UnitConversionError
return Sass::Script::Bool.new(false)
end
Sass::Script::Bool.new(this.value == other.value)
end
|
#gt(other) ⇒ Boolean
The SassScript >
operation.
178 179 180 181 |
# File 'lib/sass/script/number.rb', line 178
def gt(other)
raise NoMethodError.new(nil, :gt) unless other.is_a?(Number)
operate(other, :>)
end
|
#gte(other) ⇒ Boolean
The SassScript >=
operation.
188 189 190 191 |
# File 'lib/sass/script/number.rb', line 188
def gte(other)
raise NoMethodError.new(nil, :gte) unless other.is_a?(Number)
operate(other, :>=)
end
|
#inspect ⇒ String
Returns a readable representation of this number.
This representation is valid CSS (and valid SassScript) as long as there is only one unit.
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/sass/script/number.rb', line 227
def inspect
value =
if self.value.is_a?(Float) && (self.value.infinite? || self.value.nan?)
self.value
elsif int?
self.value.to_i
else
(self.value * PRECISION).round / PRECISION
end
"#{value}#{unit_str}"
end
|
#int? ⇒ Boolean
Returns Whether or not this number is an integer.
247 248 249 |
# File 'lib/sass/script/number.rb', line 247
def int?
value % 1 == 0.0
end
|
#legal_units? ⇒ Boolean
Returns Whether or not this number has units that can be represented in CSS (that is, zero or one #numerator_units).
258 259 260 |
# File 'lib/sass/script/number.rb', line 258
def legal_units?
(numerator_units.empty? || numerator_units.size == 1) && denominator_units.empty?
end
|
#lt(other) ⇒ Boolean
The SassScript <
operation.
198 199 200 201 |
# File 'lib/sass/script/number.rb', line 198
def lt(other)
raise NoMethodError.new(nil, :lt) unless other.is_a?(Number)
operate(other, :<)
end
|
#lte(other) ⇒ Boolean
The SassScript <=
operation.
208 209 210 211 |
# File 'lib/sass/script/number.rb', line 208
def lte(other)
raise NoMethodError.new(nil, :lte) unless other.is_a?(Number)
operate(other, :<=)
end
|
#minus(other) ⇒ Literal
The SassScript binary -
operation (e.g. !a - !b
).
Its functionality depends on the type of its argument:
Sass::Script::Number : Subtracts this number from the other, converting units if possible.
Literal : See Literal#minus.
80 81 82 83 84 85 86 |
# File 'lib/sass/script/number.rb', line 80
def minus(other)
if other.is_a? Number
operate(other, :-)
else
super
end
end
|
#mod(other) ⇒ Number
The SassScript %
operation.
142 143 144 145 146 147 148 149 150 151 |
# File 'lib/sass/script/number.rb', line 142
def mod(other)
if other.is_a?(Number)
unless other.unitless?
raise Sass::UnitConversionError.new("Cannot modulo by a number with units: #{other.inspect}.")
end
operate(other, :%)
else
raise NoMethodError.new(nil, :mod)
end
end
|
#normalize! ⇒ Object (protected)
340 341 342 343 344 345 346 347 348 349 350 351 |
# File 'lib/sass/script/number.rb', line 340
def normalize!
return if unitless?
@numerator_units, @denominator_units = sans_common_units(numerator_units, denominator_units)
@denominator_units.each_with_index do |d, i|
if convertable?(d) && (u = @numerator_units.detect(&method(:convertable?)))
@value /= conversion_factor(d, u)
@denominator_units.delete_at(i)
@numerator_units.delete_at(@numerator_units.index(u))
end
end
end
|
#operate(other, operation) ⇒ Object (protected)
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/sass/script/number.rb', line 289
def operate(other, operation)
this = self
if [:+, :-, :<=, :<, :>, :>=].include?(operation)
if unitless?
this = this.coerce(other.numerator_units, other.denominator_units)
else
other = other.coerce(numerator_units, denominator_units)
end
end
# avoid integer division
value = (:/ == operation) ? this.value.to_f : this.value
result = value.send(operation, other.value)
if result.is_a?(Numeric)
Number.new(result, *compute_units(this, other, operation))
else # Boolean op
Bool.new(result)
end
end
|
#plus(other) ⇒ Literal
The SassScript +
operation.
Its functionality depends on the type of its argument:
Sass::Script::Number : Adds the two numbers together, converting units if possible.
Color : Adds this number to each of the RGB color channels.
Literal : See Literal#plus.
58 59 60 61 62 63 64 65 66 |
# File 'lib/sass/script/number.rb', line 58
def plus(other)
if other.is_a? Number
operate(other, :+)
elsif other.is_a?(Color)
other.plus(self)
else
super
end
end
|
#sans_common_units(units1, units2) ⇒ Object (protected)
371 372 373 374 375 376 377 378 379 |
# File 'lib/sass/script/number.rb', line 371
def sans_common_units(units1, units2)
units2 = units2.dup
# Can't just use -, because we want px*px to coerce properly to px*mm
return units1.map do |u|
next u unless j = units2.index(u)
units2.delete_at(j)
nil
end.compact, units2
end
|
#times(other) ⇒ Number, Color
The SassScript *
operation.
Its functionality depends on the type of its argument:
Sass::Script::Number : Multiplies the two numbers together, converting units appropriately.
Color : Multiplies each of the RGB color channels by this number.
107 108 109 110 111 112 113 114 115 |
# File 'lib/sass/script/number.rb', line 107
def times(other)
if other.is_a? Number
self.operate(other, :*)
elsif other.is_a? Color
other.times(self)
else
raise NoMethodError.new(nil, :times)
end
end
|
#to_i ⇒ Fixnum
Returns The integer value of the number.
241 242 243 244 |
# File 'lib/sass/script/number.rb', line 241
def to_i
super unless int?
return value
end
|
#to_s ⇒ String
Returns The CSS representation of this number.
216 217 218 219 |
# File 'lib/sass/script/number.rb', line 216
def to_s
raise Sass::SyntaxError.new("#{inspect} isn't a valid CSS value.") unless legal_units?
inspect
end
|
#unary_minus ⇒ Number
The SassScript unary -
operation (e.g. -!a
).
91 92 93 |
# File 'lib/sass/script/number.rb', line 91
def unary_minus
Number.new(-value, numerator_units, denominator_units)
end
|
#unit_str ⇒ Object (protected)
331 332 333 334 335 336 337 338 |
# File 'lib/sass/script/number.rb', line 331
def unit_str
rv = numerator_units.join("*")
if denominator_units.any?
rv << "/"
rv << denominator_units.join("*")
end
rv
end
|
#unitless? ⇒ Boolean
Returns Whether or not this number has no units.
252 253 254 |
# File 'lib/sass/script/number.rb', line 252
def unitless?
numerator_units.empty? && denominator_units.empty?
end
|