Class: SassC::Script::Value::List
- Inherits:
-
SassC::Script::Value
- Object
- SassC::Script::Value
- SassC::Script::Value::List
- 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
-
#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 SassC::Script::Value
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 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.
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
#bracketed ⇒ Boolean (readonly)
Whether the list is surrounded by square brackets.
23 24 25 |
# File 'lib/sassc/script/value/list.rb', line 23 def bracketed @bracketed end |
#separator ⇒ Symbol (readonly)
The operator separating the values of the list. Either ‘:comma` or `:space`.
18 19 20 |
# File 'lib/sassc/script/value/list.rb', line 18 def separator @separator end |
#value ⇒ Array<Value> (readonly) Also known as: to_a
The Ruby array containing the contents of the list.
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.
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
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 |
#hash ⇒ Object
50 51 52 |
# File 'lib/sassc/script/value/list.rb', line 50 def hash @hash ||= [value, separator, bracketed].hash end |
#inspect ⇒ Object
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 () super value.each {|v| v. = } end |
#to_h ⇒ Object
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 |