Class: SassC::Script::Value

Inherits:
Object
  • Object
show all
Defined in:
lib/sassc/script/value.rb

Overview

The abstract superclass for SassScript objects. Many of these methods, especially the ones that correspond to SassScript operations, are designed to be overridden by subclasses which may change the semantics somewhat. The operations listed here are just the defaults.

Direct Known Subclasses

Bool, Color, List, Map, Number, String

Defined Under Namespace

Classes: Bool, Color, List, Map, Number, String

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Value

Creates a new value.



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

def initialize(value = nil)
  value.freeze unless value.nil? || value == true || value == false
  @value = value
  @options = nil
end

Instance Attribute Details

#optionsObject

Returns the options hash for this node. Raises SassC::SyntaxError if the value was created outside of the parser and #to_s was called on it

Raises:



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

def options
  return @options if @options
  raise SassC::SyntaxError.new("The #options attribute is not set on this #{self.class}. This error is probably occurring because #to_s was called on this value within a custom Sass function without first setting the #options attribute.")
end

#source_rangeObject

The source range in the document on which this node appeared.



15
16
17
# File 'lib/sassc/script/value.rb', line 15

def source_range
  @source_range
end

#valueObject (readonly)

Returns the pure Ruby value of the value. The type of this value varies based on the subclass.



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

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object

Compares this object to ‘other`



59
60
61
# File 'lib/sassc/script/value.rb', line 59

def ==(other)
  self.class == other.class && value == other.value
end

#assert_int!Object

Raises:



70
# File 'lib/sassc/script/value.rb', line 70

def assert_int!; to_i; end

#bracketedObject

Whether the value is surrounded by square brackets. For non-list values, this will be ‘false`.



80
81
82
# File 'lib/sassc/script/value.rb', line 80

def bracketed
  false
end

#eql?(other) ⇒ Boolean

True if this Value is the same as ‘other`

Returns:

  • (Boolean)


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

def eql?(other)
  self == other
end

#hashObject

Returns the hash code of this value. Two objects’ hash codes should be equal if the objects are equal.



39
40
41
# File 'lib/sassc/script/value.rb', line 39

def hash
  value.hash
end

#inspectObject

Returns a system inspect value for this object



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

def inspect
  value.inspect
end

#null?Boolean

Returns ‘false` (all Values are truthy)

Returns:

  • (Boolean)


112
113
114
# File 'lib/sassc/script/value.rb', line 112

def null?
  false
end

#separatorObject

Returns the separator for this value. For non-list-like values or the empty list, this will be ‘nil`. For lists or maps, it will be `:space` or `:comma`.



74
75
76
# File 'lib/sassc/script/value.rb', line 74

def separator
  nil
end

#to_aObject

Returns the value of this Value as an array. Single Values are considered the same as single-element arrays.



86
87
88
# File 'lib/sassc/script/value.rb', line 86

def to_a
  [self]
end

#to_boolObject

Returns ‘true` (all Values are truthy)



54
55
56
# File 'lib/sassc/script/value.rb', line 54

def to_bool
  true
end

#to_hHash<Value, Value>

Returns the value of this value as a hash. Most values don’t have hash representations, but [Map]s and empty [List]s do.

Returns:

Raises:



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

def to_h
  raise SassC::SyntaxError.new("#{inspect} is not a map.")
end

#to_iObject

Returns the integer value of this value. Raises SassC::SyntaxError if this value doesn’t implment integer conversion.

Raises:



65
66
67
# File 'lib/sassc/script/value.rb', line 65

def to_i
  raise SassC::SyntaxError.new("#{inspect} is not an integer.")
end

#to_s(opts = {}) ⇒ String Also known as: to_sass

Returns the string representation of this value as it would be output to the CSS document.

Returns:



106
107
108
# File 'lib/sassc/script/value.rb', line 106

def to_s(opts = {})
  SassC::Util.abstract(self)
end

#with_contents(contents, separator: self.separator, bracketed: self.bracketed) ⇒ Sass::Script::Value::List

Creates a new list containing ‘contents` but with the same brackets and separators as this object, when interpreted as a list.

Parameters:

  • contents (Array<Value>)

    The contents of the new list.

  • separator (Symbol) (defaults to: self.separator)

    The separator of the new list. Defaults to #separator.

  • bracketed (Boolean) (defaults to: self.bracketed)

    Whether the new list is bracketed. Defaults to #bracketed.

Returns:

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


123
124
125
# File 'lib/sassc/script/value.rb', line 123

def with_contents(contents, separator: self.separator, bracketed: self.bracketed)
  SassC::Script::Value::List.new(contents, separator: separator, bracketed: bracketed)
end