Class: Sass::Script::Value::List
- Inherits:
-
Base
- Object
- Base
- Sass::Script::Value::List
- Defined in:
- .ruby-sass/lib/sass/script/value/list.rb
Overview
A SassScript object representing a CSS list. This includes both comma-separated lists and space-separated lists.
Direct Known Subclasses
Direct Known Subclasses
Instance Attribute Summary (collapse)
-
#bracketed ⇒ Boolean
readonly
Whether the list is surrounded by square brackets.
-
#separator ⇒ Symbol
readonly
The operator separating the values of the list.
-
#value ⇒ Array<Value>
(also: #to_a)
readonly
The Ruby array containing the contents of the list.
Attributes inherited from Base
Class Method Summary (collapse)
-
.assert_valid_index(list, n) ⇒ Object
Asserts an index is within the list.
Instance Method Summary (collapse)
- #eq(other) ⇒ Object
- #hash ⇒ Object
-
#initialize(value, separator: nil, bracketed: false) ⇒ List
constructor
Creates a new list.
- #inspect ⇒ Object
- #options=(options) ⇒ Object
- #to_h ⇒ Object
- #to_s(opts = {}) ⇒ Object
- #to_sass(opts = {}) ⇒ Object
Methods inherited from Base
#==, #assert_int!, #div, #eql?, #minus, #neq, #null?, #plus, #single_eq, #to_bool, #to_i, #unary_div, #unary_minus, #unary_not, #unary_plus, #with_contents
Constructor Details
#initialize(value, separator: nil, bracketed: false) ⇒ List
Creates a new list.
27 28 29 30 31 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 27 def initialize(value, separator: nil, bracketed: false) super(value) @separator = separator @bracketed = bracketed end |
Constructor Details
#initialize(value, separator: nil, bracketed: false) ⇒ List
Creates a new list.
27 28 29 30 31 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 27 def initialize(value, separator: nil, bracketed: false) super(value) @separator = separator @bracketed = bracketed end |
Instance Attribute Details
#bracketed ⇒ Boolean (readonly)
Whether the list is surrounded by square brackets.
20 21 22 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 20 def bracketed @bracketed end |
#separator ⇒ Symbol (readonly)
The operator separating the values of the list. Either `:comma` or `:space`.
15 16 17 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 15 def separator @separator end |
#value ⇒ Array<Value> (readonly) Also known as: to_a
The Ruby array containing the contents of the list.
8 9 10 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 8 def value @value end |
Class Method Details
.assert_valid_index(list, n) ⇒ Object
Asserts an index is within the list.
102 103 104 105 106 107 108 109 110 111 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 102 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
40 41 42 43 44 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 40 def eq(other) Sass::Script::Value::Bool.new( other.is_a?(List) && value == other.value && separator == other.separator && bracketed == other.bracketed) end |
#hash ⇒ Object
46 47 48 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 46 def hash @hash ||= [value, separator, bracketed].hash end |
#inspect ⇒ Object
90 91 92 93 94 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 90 def inspect (bracketed ? '[' : '(') + value.map {|e| e.inspect}.join(sep_str(nil)) + (bracketed ? ']' : ')') end |
#options=(options) ⇒ Object
34 35 36 37 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 34 def () super value.each {|v| v. = } end |
#to_h ⇒ Object
84 85 86 87 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 84 def to_h return {} if value.empty? super end |
#to_s(opts = {}) ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 51 def to_s(opts = {}) if !bracketed && value.empty? raise Sass::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
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File '.ruby-sass/lib/sass/script/value/list.rb', line 65 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 |