Class: Sass::Value::Number
- Inherits:
-
Object
- Object
- Sass::Value::Number
show all
- Includes:
- Sass::Value
- Defined in:
- lib/sass/value/number.rb,
lib/sass/value/number/unit.rb
Overview
Instance Attribute Summary collapse
Instance Method Summary
collapse
-
#==(other) ⇒ ::Boolean
-
#assert_between(min, max, name = nil) ⇒ Numeric
-
#assert_integer(name = nil) ⇒ Integer
-
#assert_number(_name = nil) ⇒ Number
-
#assert_unit(unit, name = nil) ⇒ Number
-
#assert_unitless(name = nil) ⇒ Number
-
#coerce(new_numerator_units, new_denominator_units, name = nil) ⇒ Number
-
#coerce_to_match(other, name = nil, other_name = nil) ⇒ Number
-
#coerce_value(new_numerator_units, new_denominator_units, name = nil) ⇒ Numeric
-
#coerce_value_to_match(other, name = nil, other_name = nil) ⇒ Numeric
-
#coerce_value_to_unit(unit, name = nil) ⇒ Numeric
-
#compatible_with_unit?(unit) ⇒ ::Boolean
-
#convert(new_numerator_units, new_denominator_units, name = nil) ⇒ Number
-
#convert_to_match(other, name = nil, other_name = nil) ⇒ Number
-
#convert_value(new_numerator_units, new_denominator_units, name = nil) ⇒ Numeric
-
#convert_value_to_match(other, name = nil, other_name = nil) ⇒ Numeric
-
#hash ⇒ Integer
-
#initialize(value, numerator_units = [], denominator_units = []) ⇒ Number
constructor
A new instance of Number.
-
#integer? ⇒ ::Boolean
-
#to_i ⇒ Integer
-
#unit?(unit) ⇒ ::Boolean
-
#unitless? ⇒ ::Boolean
-
#units? ⇒ ::Boolean
#[], #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, numerator_units = [], denominator_units = []) ⇒ Number
Returns a new instance of Number.
14
15
16
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
|
# File 'lib/sass/value/number.rb', line 14
def initialize(value, numerator_units = [], denominator_units = [])
numerator_units = [numerator_units] if numerator_units.is_a?(::String)
denominator_units = [denominator_units] if denominator_units.is_a?(::String)
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_units ⇒ Array<::String>
52
53
54
|
# File 'lib/sass/value/number.rb', line 52
def denominator_units
@denominator_units
end
|
#numerator_units ⇒ Array<::String>
52
53
54
|
# File 'lib/sass/value/number.rb', line 52
def numerator_units
@numerator_units
end
|
#value ⇒ Numeric
49
50
51
|
# File 'lib/sass/value/number.rb', line 49
def value
@value
end
|
Instance Method Details
#==(other) ⇒ ::Boolean
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/sass/value/number.rb', line 55
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
148
149
150
|
# File 'lib/sass/value/number.rb', line 148
def assert_between(min, max, name = nil)
FuzzyMath.assert_between(value, min, max, name)
end
|
#assert_integer(name = nil) ⇒ Integer
133
134
135
136
137
|
# File 'lib/sass/value/number.rb', line 133
def assert_integer(name = nil)
raise error "#{self} is not an integer", name unless integer?
to_i
end
|
#assert_number(_name = nil) ⇒ Number
231
232
233
|
# File 'lib/sass/value/number.rb', line 231
def assert_number(_name = nil)
self
end
|
#assert_unit(unit, name = nil) ⇒ Number
120
121
122
123
124
|
# File 'lib/sass/value/number.rb', line 120
def assert_unit(unit, name = nil)
raise error "Expected #{self} to have no unit \"#{unit}\"", name unless unit?(unit)
self
end
|
#assert_unitless(name = nil) ⇒ Number
100
101
102
103
104
|
# File 'lib/sass/value/number.rb', line 100
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
194
195
196
197
|
# File 'lib/sass/value/number.rb', line 194
def coerce(new_numerator_units, new_denominator_units, name = nil)
Number.new(coerce_value(new_numerator_units, new_denominator_units, name), new_numerator_units,
new_denominator_units)
end
|
#coerce_to_match(other, name = nil, other_name = nil) ⇒ Number
216
217
218
|
# File 'lib/sass/value/number.rb', line 216
def coerce_to_match(other, name = nil, other_name = nil)
Number.new(coerce_value_to_match(other, name, other_name), other.numerator_units, other.denominator_units)
end
|
#coerce_value(new_numerator_units, new_denominator_units, name = nil) ⇒ Numeric
202
203
204
205
206
|
# File 'lib/sass/value/number.rb', line 202
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
222
223
224
225
226
227
228
|
# File 'lib/sass/value/number.rb', line 222
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
210
211
212
|
# File 'lib/sass/value/number.rb', line 210
def coerce_value_to_unit(unit, name = nil)
coerce_value([unit], [], name)
end
|
#compatible_with_unit?(unit) ⇒ ::Boolean
154
155
156
|
# File 'lib/sass/value/number.rb', line 154
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
161
162
163
164
|
# File 'lib/sass/value/number.rb', line 161
def convert(new_numerator_units, new_denominator_units, name = nil)
Number.new(convert_value(new_numerator_units, new_denominator_units, name), new_numerator_units,
new_denominator_units)
end
|
#convert_to_match(other, name = nil, other_name = nil) ⇒ Number
177
178
179
|
# File 'lib/sass/value/number.rb', line 177
def convert_to_match(other, name = nil, other_name = nil)
Number.new(convert_value_to_match(other, name, other_name), other.numerator_units, other.denominator_units)
end
|
#convert_value(new_numerator_units, new_denominator_units, name = nil) ⇒ Numeric
169
170
171
172
173
|
# File 'lib/sass/value/number.rb', line 169
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
183
184
185
186
187
188
189
|
# File 'lib/sass/value/number.rb', line 183
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
|
#hash ⇒ Integer
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/sass/value/number.rb', line 79
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
127
128
129
|
# File 'lib/sass/value/number.rb', line 127
def integer?
FuzzyMath.integer?(value)
end
|
#to_i ⇒ Integer
140
141
142
|
# File 'lib/sass/value/number.rb', line 140
def to_i
FuzzyMath.to_i(value)
end
|
#unit?(unit) ⇒ ::Boolean
113
114
115
|
# File 'lib/sass/value/number.rb', line 113
def unit?(unit)
single_unit? && numerator_units.first == unit
end
|
#unitless? ⇒ ::Boolean
94
95
96
|
# File 'lib/sass/value/number.rb', line 94
def unitless?
numerator_units.empty? && denominator_units.empty?
end
|
#units? ⇒ ::Boolean
107
108
109
|
# File 'lib/sass/value/number.rb', line 107
def units?
!unitless?
end
|