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

Instance Method Summary collapse

Methods inherited from Literal

#==, #and, #comma, #concat, #neq, #or, #perform, #to_bool, #unary_div, #unary_not

Methods inherited from Node

#perform

Constructor Details

#initialize(value, numerator_units = [], denominator_units = []) ⇒ 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_unitsArray<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_unitsArray<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

#valueNumeric (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

#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
# File 'lib/sass/script/number.rb', line 157

def eq(other)
  Sass::Script::Bool.new(super.to_bool &&
    self.numerator_units.sort == other.numerator_units.sort &&
    self.denominator_units.sort == other.denominator_units.sort)
end

#gt(other) ⇒ Boolean

The SassScript ‘>` operation.

Raises:

  • (NoMethodError)

    if ‘other` is an invalid type



168
169
170
171
# File 'lib/sass/script/number.rb', line 168

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

#gte(other) ⇒ Boolean

The SassScript ‘>=` operation.

Raises:

  • (NoMethodError)

    if ‘other` is an invalid type



178
179
180
181
# File 'lib/sass/script/number.rb', line 178

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

#inspectString

Returns a readable representation of this number.

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



217
218
219
220
221
222
223
224
225
226
227
# File 'lib/sass/script/number.rb', line 217

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



237
238
239
# File 'lib/sass/script/number.rb', line 237

def int?
  value % 1 == 0.0
end


248
249
250
# File 'lib/sass/script/number.rb', line 248

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

#lt(other) ⇒ Boolean

The SassScript ‘<` operation.

Raises:

  • (NoMethodError)

    if ‘other` is an invalid type



188
189
190
191
# File 'lib/sass/script/number.rb', line 188

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

#lte(other) ⇒ Boolean

The SassScript ‘<=` operation.

Raises:

  • (NoMethodError)

    if ‘other` is an invalid type



198
199
200
201
# File 'lib/sass/script/number.rb', line 198

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.

Raises:



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.

Raises:

  • (NoMethodError)

    if ‘other` is an invalid type

  • (Sass::SyntaxError)

    if ‘other` has any units



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::SyntaxError.new("Cannot modulo by a number with units: #{other.inspect}.")
    end
    operate(other, :%)
  else
    raise NoMethodError.new(nil, :mod)
  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.

Raises:



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

#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.

Raises:

  • (NoMethodError)

    if ‘other` is an invalid type



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_iFixnum

Returns The integer value of the number.

Raises:



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

def to_i
  super unless int?
  return value
end

#to_sString

Returns 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`)



206
207
208
209
# File 'lib/sass/script/number.rb', line 206

def to_s
  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`).



91
92
93
# File 'lib/sass/script/number.rb', line 91

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

#unitless?Boolean



242
243
244
# File 'lib/sass/script/number.rb', line 242

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