Class: Sass::Script::Number

Inherits:
Literal show all
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 as 3.142.

1000.0

Instance Attribute Summary collapse

Attributes inherited from Node

#context, #line, #options

Instance Method Summary collapse

Methods inherited from Literal

#==, #_perform, #and, #assert_int!, #children, #comma, #concat, #neq, #options, #or, #single_eq, #to_bool, #unary_div, #unary_not

Methods inherited from Node

#_perform, #children, #dasherize, #perform

Constructor Details

#initialize(value, numerator_units = [], denominator_units = []) ⇒ Number

Returns a new instance of Number.

Parameters:



38
39
40
41
42
43
# File 'lib/sass/script/number.rb', line 38

def initialize(value, numerator_units = [], denominator_units = [])
  super(value)
  @numerator_units = numerator_units
  @denominator_units = denominator_units
  normalize!
end

Instance Attribute Details

#denominator_unitsArray<String> (readonly)

A list of units in the denominator of the number. For example, 1px*em/in*cm would return ["in", "cm"]

Returns:



26
27
28
# File 'lib/sass/script/number.rb', line 26

def denominator_units
  @denominator_units
end

#numerator_unitsArray<String> (readonly)

A list of units in the numerator of the number. For example, 1px*em/in*cm would return ["px", "em"]

Returns:



21
22
23
# File 'lib/sass/script/number.rb', line 21

def numerator_units
  @numerator_units
end

#original

Returns the value of attribute original.



28
29
30
# File 'lib/sass/script/number.rb', line 28

def original
  @original
end

#valueNumeric (readonly)

The Ruby value of the number.

Returns:

  • (Numeric)


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.

Parameters:

Returns:

  • (Number)

    The number with the new units

Raises:



293
294
295
296
297
298
299
300
# File 'lib/sass/script/number.rb', line 293

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) (protected)



348
349
350
351
352
353
354
355
356
357
# File 'lib/sass/script/number.rb', line 348

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

#comparable_to?(other) ⇒ Boolean

Returns Whether or not this number can be compared with the other.

Parameters:

  • other (Number)

    A number to decide if it can be compared with this number.

Returns:

  • (Boolean)

    Whether or not this number can be compared with the other.



304
305
306
307
308
309
310
311
# File 'lib/sass/script/number.rb', line 304

def comparable_to?(other)
  begin
    self.operate(other, :+)
    true
  rescue Sass::UnitConversionError
    false
  end
end

#compute_units(this, other, operation) (protected)



359
360
361
362
363
364
365
366
367
368
# File 'lib/sass/script/number.rb', line 359

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) (protected)



393
394
395
396
397
# File 'lib/sass/script/number.rb', line 393

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)

Returns:

  • (Boolean)


399
400
401
# File 'lib/sass/script/number.rb', line 399

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.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Literal)

    The result of the operation



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/sass/script/number.rb', line 137

def div(other)
  if other.is_a? Number
    res = operate(other, :/)
    if self.original && other.original && context != :equals
      res.original = "#{self.original}/#{other.original}"
    end
    res
  else
    super
  end
end

#eq(other) ⇒ Boolean

The SassScript == operation.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Boolean)

    Whether this number is equal to the other object



170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/sass/script/number.rb', line 170

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.

Parameters:

  • other (Number)

    The right-hand side of the operator

Returns:

  • (Boolean)

    Whether this number is greater than the other

Raises:

  • (NoMethodError)

    if other is an invalid type



191
192
193
194
# File 'lib/sass/script/number.rb', line 191

def gt(other)
  raise NoMethodError.new(nil, :gt) unless other.is_a?(Number)
  operate(other, :>)
end

#gte(other) ⇒ Boolean

The SassScript >= operation.

Parameters:

  • other (Number)

    The right-hand side of the operator

Returns:

  • (Boolean)

    Whether this number is greater than or equal to the other

Raises:

  • (NoMethodError)

    if other is an invalid type



201
202
203
204
# File 'lib/sass/script/number.rb', line 201

def gte(other)
  raise NoMethodError.new(nil, :gte) unless other.is_a?(Number)
  operate(other, :>=)
end

#inspect(opts = {}) ⇒ String Also known as: to_sass

Returns a readable representation of this number.

This representation is valid CSS (and valid SassScript) as long as there is only one unit.

Returns:

  • (String)

    The representation



241
242
243
244
245
246
247
248
249
250
251
# File 'lib/sass/script/number.rb', line 241

def inspect(opts = {})
  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.

Returns:

  • (Boolean)

    Whether or not this number is an integer.



262
263
264
# File 'lib/sass/script/number.rb', line 262

def int?
  value % 1 == 0.0
end

Returns Whether or not this number has units that can be represented in CSS (that is, zero or one #numerator_units).

Returns:

  • (Boolean)

    Whether or not this number has units that can be represented in CSS (that is, zero or one #numerator_units).



273
274
275
# File 'lib/sass/script/number.rb', line 273

def legal_units?
  (numerator_units.empty? || numerator_units.size == 1) && denominator_units.empty?
end

#lt(other) ⇒ Boolean

The SassScript < operation.

Parameters:

  • other (Number)

    The right-hand side of the operator

Returns:

  • (Boolean)

    Whether this number is less than the other

Raises:

  • (NoMethodError)

    if other is an invalid type



211
212
213
214
# File 'lib/sass/script/number.rb', line 211

def lt(other)
  raise NoMethodError.new(nil, :lt) unless other.is_a?(Number)
  operate(other, :<)
end

#lte(other) ⇒ Boolean

The SassScript <= operation.

Parameters:

  • other (Number)

    The right-hand side of the operator

Returns:

  • (Boolean)

    Whether this number is less than or equal to the other

Raises:

  • (NoMethodError)

    if other is an invalid type



221
222
223
224
# File 'lib/sass/script/number.rb', line 221

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.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Literal)

    The result of the operation

Raises:



82
83
84
85
86
87
88
# File 'lib/sass/script/number.rb', line 82

def minus(other)
  if other.is_a? Number
    operate(other, :-)
  else
    super
  end
end

#mod(other) ⇒ Number

The SassScript % operation.

Parameters:

  • other (Number)

    The right-hand side of the operator

Returns:

  • (Number)

    This number modulo the other

Raises:



155
156
157
158
159
160
161
162
163
164
# File 'lib/sass/script/number.rb', line 155

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! (protected)



370
371
372
373
374
375
376
377
378
379
380
381
# File 'lib/sass/script/number.rb', line 370

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) (protected)



328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/sass/script/number.rb', line 328

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.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Literal)

    The result of the operation

Raises:



60
61
62
63
64
65
66
67
68
# File 'lib/sass/script/number.rb', line 60

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) (protected)



403
404
405
406
407
408
409
410
411
# File 'lib/sass/script/number.rb', line 403

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.

Parameters:

  • other (Number, Color)

    The right-hand side of the operator

Returns:

Raises:

  • (NoMethodError)

    if other is an invalid type



116
117
118
119
120
121
122
123
124
# File 'lib/sass/script/number.rb', line 116

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_iFixnum

Returns The integer value of the number.

Returns:

  • (Fixnum)

    The integer value of the number

Raises:



256
257
258
259
# File 'lib/sass/script/number.rb', line 256

def to_i
  super unless int?
  return value
end

#to_sString

Returns The CSS representation of this number.

Returns:

  • (String)

    The CSS representation of this number

Raises:

  • (Sass::SyntaxError)

    if this number has units that can't be used in CSS (e.g. px*in)



229
230
231
232
233
# File 'lib/sass/script/number.rb', line 229

def to_s
  return original if original
  raise Sass::SyntaxError.new("#{inspect} isn't a valid CSS value.") unless legal_units?
  inspect
end

#unary_minusNumber

The SassScript unary - operation (e.g. -$a).

Returns:

  • (Number)

    The negative value of this number



100
101
102
# File 'lib/sass/script/number.rb', line 100

def unary_minus
  Number.new(-value, numerator_units, denominator_units)
end

#unary_plusNumber

The SassScript unary + operation (e.g. +$a).

Returns:

  • (Number)

    The value of this number



93
94
95
# File 'lib/sass/script/number.rb', line 93

def unary_plus
  self
end

#unit_strString

Returns a human readable representation of the units in this number. For complex units this takes the form of: numerator_unit1 * numerator_unit2 / denominator_unit1 * denominator_unit2

Returns:

  • (String)

    a string that represents the units in this number



317
318
319
320
321
322
323
324
# File 'lib/sass/script/number.rb', line 317

def unit_str
  rv = numerator_units.sort.join("*")
  if denominator_units.any?
    rv << "/"
    rv << denominator_units.sort.join("*")
  end
  rv
end

#unitless?Boolean

Returns Whether or not this number has no units.

Returns:

  • (Boolean)

    Whether or not this number has no units.



267
268
269
# File 'lib/sass/script/number.rb', line 267

def unitless?
  numerator_units.empty? && denominator_units.empty?
end