Class: Sass::Script::Tree::MapLiteral
- Defined in:
- lib/sass/script/tree/map_literal.rb
Overview
A class representing a map literal. When resolved, this returns a Node::Map.
Instance Attribute Summary collapse
-
#pairs ⇒ Array<(Node, Node)>
readonly
The key/value pairs that make up this map node.
Attributes inherited from Node
#filename, #line, #options, #source_range
Instance Method Summary collapse
- #_perform(environment) protected
- #children
- #deep_copy
-
#initialize(pairs) ⇒ MapLiteral
constructor
Creates a new map literal.
- #to_sass(opts = {}) (also: #inspect)
Methods inherited from Node
#dasherize, #force_division!, #opts, #perform
Constructor Details
#initialize(pairs) ⇒ MapLiteral
Creates a new map literal.
14 15 16 |
# File 'lib/sass/script/tree/map_literal.rb', line 14
def initialize(pairs)
@pairs = pairs
end
|
Instance Attribute Details
Instance Method Details
#_perform(environment) (protected)
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/sass/script/tree/map_literal.rb', line 50
def _perform(environment)
keys = Set.new
map = Sass::Script::Value::Map.new(Hash[pairs.map do |(k, v)|
k, v = k.perform(environment), v.perform(environment)
if keys.include?(k)
raise Sass::SyntaxError.new("Duplicate key #{k.inspect} in map #{to_sass}.")
end
keys << k
[k, v]
end])
map.options = options
map
end
|
#children
19 20 21 |
# File 'lib/sass/script/tree/map_literal.rb', line 19
def children
@pairs.flatten
end
|
#deep_copy
40 41 42 43 44 45 |
# File 'lib/sass/script/tree/map_literal.rb', line 40
def deep_copy
node = dup
node.instance_variable_set('@pairs',
pairs.map {|(k, v)| [k.deep_copy, v.deep_copy]})
node
end
|
#to_sass(opts = {}) Also known as: inspect
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sass/script/tree/map_literal.rb', line 24
def to_sass(opts = {})
return "()" if pairs.empty?
to_sass = lambda do |value|
if value.is_a?(ListLiteral) && value.separator == :comma
"(#{value.to_sass(opts)})"
else
value.to_sass(opts)
end
end
"(" + pairs.map {|(k, v)| "#{to_sass[k]}: #{to_sass[v]}"}.join(', ') + ")"
end
|