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
- NO_UNITS =
Used so we don't allocate two new arrays for each new number.
[]
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.
-
#original ⇒ Boolean?
The original representation of this number.
-
#value ⇒ Numeric
readonly
The Ruby value of the number.
Attributes inherited from Node
Instance Method Summary collapse
-
#coerce(num_units, den_units) ⇒ Number
Returns this number converted to other units.
-
#comparable_to?(other) ⇒ Boolean
Whether or not this number can be compared with the other.
-
#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 = NO_UNITS, denominator_units = NO_UNITS) ⇒ Number
constructor
A new instance of Number.
-
#inspect(opts = {}) ⇒ String
(also: #to_sass)
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. -
#plus(other) ⇒ Literal
The SassScript
+
operation. -
#times(other) ⇒ Number, Color
The SassScript
*
operation. -
#to_i ⇒ Fixnum
The integer value of the number.
-
#to_s(opts = {}) ⇒ String
The CSS representation of this number.
-
#unary_minus ⇒ Number
The SassScript unary
-
operation (e.g.-$a
). -
#unary_plus ⇒ Number
The SassScript unary
+
operation (e.g.+$a
). -
#unit_str ⇒ String
Returns a human readable representation of the units in this number.
-
#unitless? ⇒ Boolean
Whether or not this number has no units.
Methods inherited from Literal
#==, #_perform, #and, #assert_int!, #children, #comma, #deep_copy, #neq, #options, #or, #single_eq, #space, #to_a, #to_bool, #unary_div, #unary_not
Methods inherited from Node
#_perform, #children, #dasherize, #deep_copy, #opts, #perform
Constructor Details
#initialize(value, numerator_units = NO_UNITS, denominator_units = NO_UNITS) ⇒ Number
Returns a new instance of Number.
50 51 52 53 54 55 |
# File 'lib/sass/script/number.rb', line 50
def initialize(value, numerator_units = NO_UNITS, denominator_units = NO_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
|
#original ⇒ Boolean?
The original representation of this number.
For example, although the result of 1px/2px
is 0.5
,
the value of #original
is "1px/2px"
.
This is only non-nil when the original value should be used as the CSS value,
as in font: 1px/2px
.
36 37 38 |
# File 'lib/sass/script/number.rb', line 36
def original
@original
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.
298 299 300 301 302 303 304 305 |
# File 'lib/sass/script/number.rb', line 298
def coerce(num_units, den_units)
Number.new(if unitless?
self.value
else
self.value * coercion_factor(@numerator_units, num_units) /
coercion_factor(@denominator_units, den_units)
end, num_units, den_units)
end
|
#comparable_to?(other) ⇒ Boolean
Returns Whether or not this number can be compared with the other.
309 310 311 312 313 314 315 316 |
# File 'lib/sass/script/number.rb', line 309
def comparable_to?(other)
begin
operate(other, :+)
true
rescue Sass::UnitConversionError
false
end
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.
149 150 151 152 153 154 155 156 157 158 159 |
# File 'lib/sass/script/number.rb', line 149
def div(other)
if other.is_a? Number
res = operate(other, :/)
if self.original && other.original
res.original = "#{self.original}/#{other.original}"
end
res
else
super
end
end
|
#eq(other) ⇒ Boolean
The SassScript ==
operation.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'lib/sass/script/number.rb', line 182
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.
203 204 205 206 |
# File 'lib/sass/script/number.rb', line 203
def gt(other)
raise NoMethodError.new(nil, :gt) unless other.is_a?(Number)
operate(other, :>)
end
|
#gte(other) ⇒ Boolean
The SassScript >=
operation.
213 214 215 216 |
# File 'lib/sass/script/number.rb', line 213
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.
253 254 255 256 |
# File 'lib/sass/script/number.rb', line 253
def inspect(opts = {})
value = self.class.round(self.value)
unitless? ? value.to_s : "#{value}#{unit_str}"
end
|
#int? ⇒ Boolean
Returns Whether or not this number is an integer.
267 268 269 |
# File 'lib/sass/script/number.rb', line 267
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).
278 279 280 |
# File 'lib/sass/script/number.rb', line 278
def legal_units?
(@numerator_units.empty? || @numerator_units.size == 1) && @denominator_units.empty?
end
|
#lt(other) ⇒ Boolean
The SassScript <
operation.
223 224 225 226 |
# File 'lib/sass/script/number.rb', line 223
def lt(other)
raise NoMethodError.new(nil, :lt) unless other.is_a?(Number)
operate(other, :<)
end
|
#lte(other) ⇒ Boolean
The SassScript <=
operation.
233 234 235 236 |
# File 'lib/sass/script/number.rb', line 233
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.
94 95 96 97 98 99 100 |
# File 'lib/sass/script/number.rb', line 94
def minus(other)
if other.is_a? Number
operate(other, :-)
else
super
end
end
|
#mod(other) ⇒ Number
The SassScript %
operation.
167 168 169 170 171 172 173 174 175 176 |
# File 'lib/sass/script/number.rb', line 167
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
|
#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.
72 73 74 75 76 77 78 79 80 |
# File 'lib/sass/script/number.rb', line 72
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.
128 129 130 131 132 133 134 135 136 |
# File 'lib/sass/script/number.rb', line 128
def times(other)
if other.is_a? Number
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.
261 262 263 264 |
# File 'lib/sass/script/number.rb', line 261
def to_i
super unless int?
return value
end
|
#to_s(opts = {}) ⇒ String
Returns The CSS representation of this number.
241 242 243 244 245 |
# File 'lib/sass/script/number.rb', line 241
def to_s(opts = {})
return original if original
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
).
112 113 114 |
# File 'lib/sass/script/number.rb', line 112
def unary_minus
Number.new(-value, @numerator_units, @denominator_units)
end
|
#unary_plus ⇒ Number
The SassScript unary +
operation (e.g. +$a
).
105 106 107 |
# File 'lib/sass/script/number.rb', line 105
def unary_plus
self
end
|
#unit_str ⇒ String
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
322 323 324 325 326 327 328 329 |
# File 'lib/sass/script/number.rb', line 322
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.
272 273 274 |
# File 'lib/sass/script/number.rb', line 272
def unitless?
@numerator_units.empty? && @denominator_units.empty?
end
|