Class: Sass::Script::Tree::MapLiteral

Inherits:
Node
  • Object
show all
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

Attributes inherited from Node

#filename, #line, #options, #source_range

Instance Method Summary collapse

Methods inherited from Node

#dasherize, #force_division!, #opts, #perform

Constructor Details

#initialize(pairs) ⇒ MapLiteral

Creates a new map literal.

Parameters:



14
15
16
# File 'lib/sass/script/tree/map_literal.rb', line 14

def initialize(pairs)
  @pairs = pairs
end

Instance Attribute Details

#pairsArray<(Node, Node)> (readonly)

The key/value pairs that make up this map node. This isn't a Hash so that we can detect key collisions once all the keys have been performed.

Returns:



9
10
11
# File 'lib/sass/script/tree/map_literal.rb', line 9

def pairs
  @pairs
end

Instance Method Details

#_perform(environment) (protected)

See Also:



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

See Also:



19
20
21
# File 'lib/sass/script/tree/map_literal.rb', line 19

def children
  @pairs.flatten
end

#deep_copy

See Also:



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

See Also:



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