Class: SassC::Script::Value::List

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

Overview

A SassScript object representing a CSS list. This includes both comma-separated lists and space-separated lists.

Instance Attribute Summary collapse

Attributes inherited from SassC::Script::Value

#options, #source_range

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from SassC::Script::Value

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

Constructor Details

#initialize(value, separator: nil, bracketed: false) ⇒ List

Creates a new list.

Parameters:



30
31
32
33
34
# File 'lib/sassc/script/value/list.rb', line 30

def initialize(value, separator: nil, bracketed: false)
  super(value)
  @separator = separator
  @bracketed = bracketed
end

Instance Attribute Details

#bracketedBoolean (readonly)

Whether the list is surrounded by square brackets.

Returns:

  • (Boolean)


23
24
25
# File 'lib/sassc/script/value/list.rb', line 23

def bracketed
  @bracketed
end

#separatorSymbol (readonly)

The operator separating the values of the list. Either ‘:comma` or `:space`.

Returns:

  • (Symbol)


18
19
20
# File 'lib/sassc/script/value/list.rb', line 18

def separator
  @separator
end

#valueArray<Value> (readonly) Also known as: to_a

The Ruby array containing the contents of the list.

Returns:



11
12
13
# File 'lib/sassc/script/value/list.rb', line 11

def value
  @value
end

Class Method Details

.assert_valid_index(list, n) ⇒ Object

Asserts an index is within the list.

Parameters:

  • list (Sass::Script::Value::List)

    The list for which the index should be checked.

  • n (Sass::Script::Value::Number)

    The index being checked.



104
105
106
107
108
109
110
111
112
113
# File 'lib/sassc/script/value/list.rb', line 104

def self.assert_valid_index(list, n)
  if !n.int? || n.to_i == 0
    raise ArgumentError.new("List index #{n} must be a non-zero integer")
  elsif list.to_a.size == 0
    raise ArgumentError.new("List index is #{n} but list has no items")
  elsif n.to_i.abs > (size = list.to_a.size)
    raise ArgumentError.new(
      "List index is #{n} but list is only #{size} item#{'s' if size != 1} long")
  end
end

Instance Method Details

#eq(other) ⇒ Object

See Also:

  • Value#eq


43
44
45
46
47
48
# File 'lib/sassc/script/value/list.rb', line 43

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

#hashObject



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

def hash
  @hash ||= [value, separator, bracketed].hash
end

#inspectObject



94
95
96
# File 'lib/sassc/script/value/list.rb', line 94

def inspect
  (bracketed ? '[' : '(') + value.map {|e| e.inspect}.join(sep_str(nil)) + (bracketed ? ']' : ')')
end

#options=(options) ⇒ Object



37
38
39
40
# File 'lib/sassc/script/value/list.rb', line 37

def options=(options)
  super
  value.each {|v| v.options = options}
end

#to_hObject



88
89
90
91
# File 'lib/sassc/script/value/list.rb', line 88

def to_h
  return {} if value.empty?
  super
end

#to_s(opts = {}) ⇒ Object



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

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

  members = value.
    reject {|e| e.is_a?(Null) || e.is_a?(List) && e.value.empty?}.
    map {|e| e.to_s(opts)}

  contents = members.join(sep_str)
  bracketed ? "[#{contents}]" : contents
end

#to_sass(opts = {}) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/sassc/script/value/list.rb', line 69

def to_sass(opts = {})
  return bracketed ? "[]" : "()" if value.empty?
  members = value.map do |v|
    if element_needs_parens?(v)
      "(#{v.to_sass(opts)})"
    else
      v.to_sass(opts)
    end
  end

  if separator == :comma && members.length == 1
    return "#{bracketed ? '[' : '('}#{members.first},#{bracketed ? ']' : ')'}"
  end

  contents = members.join(sep_str(nil))
  bracketed ? "[#{contents}]" : contents
end