Class: SassC::Script::Value::Map

Inherits:
SassC::Script::Value show all
Defined in:
lib/sassc/script/value/map.rb

Instance Attribute Summary collapse

Attributes inherited from SassC::Script::Value

#options, #source_range

Instance Method Summary collapse

Methods inherited from SassC::Script::Value

#==, #assert_int!, #bracketed, #eql?, #null?, #to_bool, #to_i, #with_contents

Constructor Details

#initialize(hash) ⇒ Map

Creates a new map.

Parameters:

  • hash (Hash<Node, Node>)


13
14
15
# File 'lib/sassc/script/value/map.rb', line 13

def initialize(hash)
  super(hash)
end

Instance Attribute Details

#valueHash<Node, Node> (readonly) Also known as: to_h

The Ruby hash containing the contents of this map.

Returns:

  • (Hash<Node, Node>)


7
8
9
# File 'lib/sassc/script/value/map.rb', line 7

def value
  @value
end

Instance Method Details

#eq(other) ⇒ Object

See Also:

  • Value#eq


41
42
43
# File 'lib/sassc/script/value/map.rb', line 41

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

#hashObject



45
46
47
# File 'lib/sassc/script/value/map.rb', line 45

def hash
  @hash ||= value.hash
end

#options=(options) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/sassc/script/value/map.rb', line 18

def options=(options)
  super
  value.each do |k, v|
    k.options = options
    v.options = options
  end
end

#separatorObject



27
28
29
# File 'lib/sassc/script/value/map.rb', line 27

def separator
  :comma unless value.empty?
end

#to_aObject



32
33
34
35
36
37
38
# File 'lib/sassc/script/value/map.rb', line 32

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

#to_s(opts = {}) ⇒ Object



50
51
52
# File 'lib/sassc/script/value/map.rb', line 50

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

#to_sass(opts = {}) ⇒ Object Also known as: inspect



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/sassc/script/value/map.rb', line 54

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