class Sass::Script::Tree::MapLiteral - sass-3.4.14 Documentation

# class Sass::Script::Tree::MapLiteral

A class representing a map literal. When resolved, this returns a {Sass::Script::Node::Map}.

### Attributes

pairs[R]

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.

@return [Array<(Node, Node)>]

### Public Class Methods

new(pairs) click to toggle source

Creates a new map literal.

@param pairs [Array<(Node, Node)>] See {#pairs}

    # File lib/sass/script/tree/map_literal.rb, line 14
    def initialize(pairs)
      @pairs = pairs
    end

### Public Instance Methods

children() click to toggle source

@see Sass::Script::Tree::Node#children

    # File lib/sass/script/tree/map_literal.rb, line 19
    def children
      @pairs.flatten
    end

deep_copy() click to toggle source

@see Sass::Script::Tree::Node#deep_copy

    # 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

inspect(opts = {})
Alias for: to_sass
to_sass(opts = {}) click to toggle source

@see Sass::Script::Tree::Node#to_sass

    # 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

Also aliased as: inspect

### Protected Instance Methods

_perform(environment) click to toggle source

@see Node#_perform

    # File lib/sass/script/tree/map_literal.rb, line 50
    def _perform(environment)
      keys = Set.new
      map = Sass::Script::Value::Map.new(Sass::Util.to_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