Class: Sass::Value::List

Inherits:
Object
  • Object
show all
Includes:
Sass::Value
Defined in:
lib/sass/value/list.rb

Overview

Sass’s list type.

Direct Known Subclasses

ArgumentList

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Sass::Value

#[], #assert_boolean, #assert_calculation, #assert_color, #assert_function, #assert_number, #assert_string, #eql?, #sass_index_to_array_index, #to_bool, #to_nil

Constructor Details

#initialize(contents = [], separator: ',', bracketed: false) ⇒ List

Returns a new instance of List.

Parameters:

  • contents (Array<Value>) (defaults to: [])
  • separator (::String) (defaults to: ',')
  • bracketed (::Boolean) (defaults to: false)


14
15
16
17
18
19
20
21
22
# File 'lib/sass/value/list.rb', line 14

def initialize(contents = [], separator: ',', bracketed: false)
  if separator.nil? && contents.length > 1
    raise Sass::ScriptError, 'A list with more than one element must have an explicit separator'
  end

  @contents = contents.freeze
  @separator = separator.freeze
  @bracketed = bracketed.freeze
end

Instance Attribute Details

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

Returns:



25
26
27
# File 'lib/sass/value/list.rb', line 25

def contents
  @contents
end

#separator::String? (readonly)

Returns:

  • (::String, nil)


28
29
30
# File 'lib/sass/value/list.rb', line 28

def separator
  @separator
end

Instance Method Details

#==(other) ⇒ ::Boolean

Returns:

  • (::Boolean)


36
37
38
39
40
41
42
# File 'lib/sass/value/list.rb', line 36

def ==(other)
  (other.is_a?(Sass::Value::List) &&
   other.contents == contents &&
   other.separator == separator &&
   other.bracketed? == bracketed?) ||
    (to_a.empty? && other.is_a?(Sass::Value::Map) && other.to_a.empty?)
end

#assert_map(name = nil) ⇒ Map

Returns:

Raises:



68
69
70
# File 'lib/sass/value/list.rb', line 68

def assert_map(name = nil)
  to_a.empty? ? Sass::Value::Map.new({}) : super.assert_map(name)
end

#at(index) ⇒ Value

Parameters:

  • index (Numeric)

Returns:



46
47
48
49
50
51
52
# File 'lib/sass/value/list.rb', line 46

def at(index)
  index = index.floor
  index = to_a.length + index if index.negative?
  return nil if index.negative? || index >= to_a.length

  to_a[index]
end

#bracketed?::Boolean

Returns:

  • (::Boolean)


31
32
33
# File 'lib/sass/value/list.rb', line 31

def bracketed?
  @bracketed
end

#hashInteger

Returns:

  • (Integer)


55
56
57
# File 'lib/sass/value/list.rb', line 55

def hash
  @hash ||= contents.hash
end

#to_mapMap?

Returns:



62
63
64
# File 'lib/sass/value/list.rb', line 62

def to_map
  to_a.empty? ? Sass::Value::Map.new({}) : nil
end