/
value.rb
128 lines (105 loc) · 2.79 KB
/
value.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# frozen_string_literal: true
require_relative 'script_error'
module Sass
# The abstract base class of Sass's value types.
#
# @see https://sass-lang.com/documentation/js-api/classes/Value
module Value
# @return [::String, nil]
def separator
nil
end
# @return [::Boolean]
def bracketed?
false
end
# @return [::Boolean]
def eql?(other)
self == other
end
# @param index [Numeric]
# @return [Value]
def [](index)
at(index)
end
# @param index [Numeric]
# @return [Value]
def at(index)
index < 1 && index >= -1 ? self : nil
end
# @return [Array<Value>]
def to_a
[self]
end
# @return [::Boolean]
def to_bool
true
end
# @return [Map, nil]
def to_map
nil
end
# @return [Value, nil]
def to_nil
self
end
# @return [Boolean]
# @raise [ScriptError]
def assert_boolean(name = nil)
raise Sass::ScriptError.new("#{self} is not a boolean", name)
end
# @raise [ScriptError]
def assert_calculation(name = nil)
raise Sass::ScriptError.new("#{self} is not a calculation", name)
end
# @return [Color]
# @raise [ScriptError]
def assert_color(name = nil)
raise Sass::ScriptError.new("#{self} is not a color", name)
end
# @return [Function]
# @raise [ScriptError]
def assert_function(name = nil)
raise Sass::ScriptError.new("#{self} is not a function", name)
end
# @return [Map]
# @raise [ScriptError]
def assert_map(name = nil)
raise Sass::ScriptError.new("#{self} is not a map", name)
end
# @return [Number]
# @raise [ScriptError]
def assert_number(name = nil)
raise Sass::ScriptError.new("#{self} is not a number", name)
end
# @return [String]
# @raise [ScriptError]
def assert_string(name = nil)
raise Sass::ScriptError.new("#{self} is not a string", name)
end
# @param sass_index [Number]
# @return [Integer]
def sass_index_to_array_index(sass_index, name = nil)
index = sass_index.assert_number(name).assert_integer(name)
raise Sass::ScriptError.new('List index may not be 0', name) if index.zero?
if index.abs > to_a_length
raise Sass::ScriptError.new("Invalid index #{sass_index} for a list with #{to_a_length} elements", name)
end
index.negative? ? to_a_length + index : index - 1
end
private
def to_a_length
1
end
end
end
require_relative 'value/list'
require_relative 'value/argument_list'
require_relative 'value/boolean'
require_relative 'value/color'
require_relative 'value/function'
require_relative 'value/fuzzy_math'
require_relative 'value/map'
require_relative 'value/null'
require_relative 'value/number'
require_relative 'value/string'