Class: Sass::Value::Number

Inherits:
Object
  • Object
show all
Includes:
Sass::Value
Defined in:
lib/sass/value/number.rb,
lib/sass/value/number/unit.rb

Overview

Sass’s number type.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Sass::Value

#[], #assert_boolean, #assert_calculation, #assert_color, #assert_function, #assert_map, #assert_string, #at, #bracketed?, #eql?, #sass_index_to_array_index, #separator, #to_a, #to_bool, #to_map, #to_nil

Constructor Details

#initialize(value, unit = nil) ⇒ Number

Returns a new instance of Number.

Parameters:

  • value (Numeric)
  • unit (::String, Hash) (defaults to: nil)

Options Hash (unit):

  • :numerator_units (Array<::String>)
  • :denominator_units (Array<::String>)


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/sass/value/number.rb', line 17

def initialize(value, unit = nil)
  case unit
  when nil
    numerator_units = []
    denominator_units = []
  when ::String
    numerator_units = [unit]
    denominator_units = []
  when ::Hash
    numerator_units = unit.fetch(:numerator_units, [])
    raise error "invalid numerator_units #{numerator_units.inspect}" unless numerator_units.is_a? Array

    denominator_units = unit.fetch(:denominator_units, [])
    raise error "invalid denominator_units #{denominator_units.inspect}" unless denominator_units.is_a? Array
  else
    raise error "invalid unit #{unit.inspect}"
  end

  unless denominator_units.empty? && numerator_units.empty?
    value = value.dup
    numerator_units = numerator_units.dup
    new_denominator_units = []

    denominator_units.each do |denominator_unit|
      index = numerator_units.find_index do |numerator_unit|
        factor = Unit.conversion_factor(denominator_unit, numerator_unit)
        if factor.nil?
          false
        else
          value *= factor
          true
        end
      end
      if index.nil?
        new_denominator_units.push(denominator_unit)
      else
        numerator_units.delete_at(index)
      end
    end

    denominator_units = new_denominator_units
  end

  @value = value.freeze
  @numerator_units = numerator_units.freeze
  @denominator_units = denominator_units.freeze
end

Instance Attribute Details

#denominator_unitsArray<::String> (readonly)

Returns:

  • (Array<::String>)


69
70
71
# File 'lib/sass/value/number.rb', line 69

def denominator_units
  @denominator_units
end

#numerator_unitsArray<::String> (readonly)

Returns:

  • (Array<::String>)


69
70
71
# File 'lib/sass/value/number.rb', line 69

def numerator_units
  @numerator_units
end

#valueNumeric (readonly)

Returns:

  • (Numeric)


66
67
68
# File 'lib/sass/value/number.rb', line 66

def value
  @value
end

Instance Method Details

#==(other) ⇒ ::Boolean

Returns:

  • (::Boolean)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sass/value/number.rb', line 72

def ==(other)
  return false unless other.is_a? Sass::Value::Number

  return false if numerator_units.length != other.numerator_units.length ||
                  denominator_units.length != other.denominator_units.length

  return FuzzyMath.equals(value, other.value) if unitless?

  if Unit.canonicalize_units(numerator_units) != Unit.canonicalize_units(other.numerator_units) &&
     Unit.canonicalize_units(denominator_units) != Unit.canonicalize_units(other.denominator_units)
    return false
  end

  FuzzyMath.equals(
    (value *
    Unit.canonical_multiplier(numerator_units) /
    Unit.canonical_multiplier(denominator_units)),
    (other.value *
    Unit.canonical_multiplier(other.numerator_units) /
    Unit.canonical_multiplier(other.denominator_units))
  )
end

#assert_between(min, max, name = nil) ⇒ Numeric

Parameters:

  • min (Numeric)
  • max (Numeric)

Returns:

  • (Numeric)

Raises:



165
166
167
# File 'lib/sass/value/number.rb', line 165

def assert_between(min, max, name = nil)
  FuzzyMath.assert_between(value, min, max, name)
end

#assert_integer(name = nil) ⇒ Integer

Returns:

  • (Integer)

Raises:



150
151
152
153
154
# File 'lib/sass/value/number.rb', line 150

def assert_integer(name = nil)
  raise error "#{self} is not an integer", name unless integer?

  to_i
end

#assert_number(_name = nil) ⇒ Number

Returns:



258
259
260
# File 'lib/sass/value/number.rb', line 258

def assert_number(_name = nil)
  self
end

#assert_unit(unit, name = nil) ⇒ Number

Parameters:

  • unit (::String)

Returns:

Raises:



137
138
139
140
141
# File 'lib/sass/value/number.rb', line 137

def assert_unit(unit, name = nil)
  raise error "Expected #{self} to have unit #{unit.inspect}", name unless unit?(unit)

  self
end

#assert_unitless(name = nil) ⇒ Number

Returns:

Raises:



117
118
119
120
121
# File 'lib/sass/value/number.rb', line 117

def assert_unitless(name = nil)
  raise error "Expected #{self} to have no units", name unless unitless?

  self
end

#coerce(new_numerator_units, new_denominator_units, name = nil) ⇒ Number

Parameters:

  • new_numerator_units (Array<::String>)
  • new_denominator_units (Array<::String>)

Returns:



216
217
218
219
220
221
# File 'lib/sass/value/number.rb', line 216

def coerce(new_numerator_units, new_denominator_units, name = nil)
  Number.new(coerce_value(new_numerator_units, new_denominator_units, name), {
               numerator_units: new_numerator_units,
               denominator_units: new_denominator_units
             })
end

#coerce_to_match(other, name = nil, other_name = nil) ⇒ Number

Parameters:

Returns:



240
241
242
243
244
245
# File 'lib/sass/value/number.rb', line 240

def coerce_to_match(other, name = nil, other_name = nil)
  Number.new(coerce_value_to_match(other, name, other_name), {
               numerator_units: other.numerator_units,
               denominator_units: other.denominator_units
             })
end

#coerce_value(new_numerator_units, new_denominator_units, name = nil) ⇒ Numeric

Parameters:

  • new_numerator_units (Array<::String>)
  • new_denominator_units (Array<::String>)

Returns:

  • (Numeric)


226
227
228
229
230
# File 'lib/sass/value/number.rb', line 226

def coerce_value(new_numerator_units, new_denominator_units, name = nil)
  coerce_or_convert_value(new_numerator_units, new_denominator_units,
                          coerce_unitless: true,
                          name: name)
end

#coerce_value_to_match(other, name = nil, other_name = nil) ⇒ Numeric

Parameters:

Returns:

  • (Numeric)


249
250
251
252
253
254
255
# File 'lib/sass/value/number.rb', line 249

def coerce_value_to_match(other, name = nil, other_name = nil)
  coerce_or_convert_value(other.numerator_units, other.denominator_units,
                          coerce_unitless: true,
                          name: name,
                          other: other,
                          other_name: other_name)
end

#coerce_value_to_unit(unit, name = nil) ⇒ Numeric

Parameters:

  • unit (::String)

Returns:

  • (Numeric)


234
235
236
# File 'lib/sass/value/number.rb', line 234

def coerce_value_to_unit(unit, name = nil)
  coerce_value([unit], [], name)
end

#compatible_with_unit?(unit) ⇒ ::Boolean

Parameters:

  • unit (::String)

Returns:

  • (::Boolean)


171
172
173
# File 'lib/sass/value/number.rb', line 171

def compatible_with_unit?(unit)
  single_unit? && !Unit.conversion_factor(numerator_units.first, unit).nil?
end

#convert(new_numerator_units, new_denominator_units, name = nil) ⇒ Number

Parameters:

  • new_numerator_units (Array<::String>)
  • new_denominator_units (Array<::String>)

Returns:



178
179
180
181
182
183
# File 'lib/sass/value/number.rb', line 178

def convert(new_numerator_units, new_denominator_units, name = nil)
  Number.new(convert_value(new_numerator_units, new_denominator_units, name), {
               numerator_units: new_numerator_units,
               denominator_units: new_denominator_units
             })
end

#convert_to_match(other, name = nil, other_name = nil) ⇒ Number

Parameters:

Returns:



196
197
198
199
200
201
# File 'lib/sass/value/number.rb', line 196

def convert_to_match(other, name = nil, other_name = nil)
  Number.new(convert_value_to_match(other, name, other_name), {
               numerator_units: other.numerator_units,
               denominator_units: other.denominator_units
             })
end

#convert_value(new_numerator_units, new_denominator_units, name = nil) ⇒ Numeric

Parameters:

  • new_numerator_units (Array<::String>)
  • new_denominator_units (Array<::String>)

Returns:

  • (Numeric)


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

def convert_value(new_numerator_units, new_denominator_units, name = nil)
  coerce_or_convert_value(new_numerator_units, new_denominator_units,
                          coerce_unitless: false,
                          name: name)
end

#convert_value_to_match(other, name = nil, other_name = nil) ⇒ Numeric

Parameters:

Returns:

  • (Numeric)


205
206
207
208
209
210
211
# File 'lib/sass/value/number.rb', line 205

def convert_value_to_match(other, name = nil, other_name = nil)
  coerce_or_convert_value(other.numerator_units, other.denominator_units,
                          coerce_unitless: false,
                          name: name,
                          other: other,
                          other_name: other_name)
end

#hashInteger

Returns:

  • (Integer)


96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/sass/value/number.rb', line 96

def hash
  @hash ||= if unitless?
              FuzzyMath.hash(value)
            elsif single_unit?
              FuzzyMath.hash(
                value * Unit.canonical_multiplier_for_unit(numerator_units.first)
              )
            else
              FuzzyMath.hash(
                value * Unit.canonical_multiplier(numerator_units) / Unit.canonical_multiplier(denominator_units)
              )
            end
end

#integer?::Boolean

Returns:

  • (::Boolean)


144
145
146
# File 'lib/sass/value/number.rb', line 144

def integer?
  FuzzyMath.integer?(value)
end

#to_iInteger

Returns:

  • (Integer)


157
158
159
# File 'lib/sass/value/number.rb', line 157

def to_i
  FuzzyMath.to_i(value)
end

#unit?(unit) ⇒ ::Boolean

Parameters:

  • unit (::String)

Returns:

  • (::Boolean)


130
131
132
# File 'lib/sass/value/number.rb', line 130

def unit?(unit)
  single_unit? && numerator_units.first == unit
end

#unitless?::Boolean

Returns:

  • (::Boolean)


111
112
113
# File 'lib/sass/value/number.rb', line 111

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

#units?::Boolean

Returns:

  • (::Boolean)


124
125
126
# File 'lib/sass/value/number.rb', line 124

def units?
  !unitless?
end