File: map.rb

package info (click to toggle)
ruby-sassc 2.4.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 416 kB
  • sloc: ruby: 2,277; makefile: 3
file content (69 lines) | stat: -rw-r--r-- 1,382 bytes parent folder | download | duplicates (3)
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
# frozen_string_literal: true

class SassC::Script::Value::Map < SassC::Script::Value

  # The Ruby hash containing the contents of this map.
  # @return [Hash<Node, Node>]
  attr_reader :value
  alias_method :to_h, :value

  # Creates a new map.
  #
  # @param hash [Hash<Node, Node>]
  def initialize(hash)
    super(hash)
  end

  # @see Value#options=
  def options=(options)
    super
    value.each do |k, v|
      k.options = options
      v.options = options
    end
  end

  # @see Value#separator
  def separator
    :comma unless value.empty?
  end

  # @see Value#to_a
  def to_a
    value.map do |k, v|
      list = SassC::Script::Value::List.new([k, v], separator: :space)
      list.options = options
      list
    end
  end

  # @see Value#eq
  def eq(other)
    SassC::Script::Value::Bool.new(other.is_a?(Map) && value == other.value)
  end

  def hash
    @hash ||= value.hash
  end

  # @see Value#to_s
  def to_s(opts = {})
    raise SassC::SyntaxError.new("#{inspect} isn't a valid CSS value.")
  end

  def to_sass(opts = {})
    return "()" if value.empty?

    to_sass = lambda do |value|
      if value.is_a?(List) && value.separator == :comma
        "(#{value.to_sass(opts)})"
      else
        value.to_sass(opts)
      end
    end

    "(#{value.map {|(k, v)| "#{to_sass[k]}: #{to_sass[v]}"}.join(', ')})"
  end
  alias_method :inspect, :to_sass

end