Class: Sass::Script::Literal

Inherits:
Node
  • Object
show all
Defined in:
/var/www/sass-pages/.sass/lib/sass/script/literal.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, Number, String

Instance Attribute Summary

Instance Method Summary

Methods inherited from Node

#dasherize, #opts, #perform

Constructor Details

- (Literal) initialize(value = nil)

Creates a new literal.

Parameters:

  • (Object) value (defaults to: nil) — The object for #value


23
24
25
26
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 23

def initialize(value = nil)
  @value = value
  super()
end

Instance Attribute Details

- (Object) value (readonly)

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

Returns:

  • (Object)


18
19
20
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 18

def value
  @value
end

Instance Method Details

- (Boolean) ==(other)

Compares this object with another.

Parameters:

  • (Object) other — The object to compare with

Returns:

  • (Boolean) — Whether or not this literal is equivalent to other


191
192
193
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 191

def ==(other)
  eq(other).to_bool
end

- (Literal) _perform(environment) (protected)

Evaluates the literal.

Parameters:

  • (Sass::Environment) environment — The environment in which to evaluate the SassScript

Returns:



227
228
229
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 227

def _perform(environment)
  self
end

- assert_int!

Raises:



202
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 202

def assert_int!; to_i end

- (Array<Node>) children

Returns an empty array.

Returns:

  • (Array<Node>) — empty

See Also:



32
33
34
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 32

def children
  []
end

- (Script::String) comma(other)

The SassScript , operation (e.g. $a, $b, "foo", "bar").

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Script::String) — A string containing both literals separated by ", "


106
107
108
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 106

def comma(other)
  Sass::Script::String.new("#{self.to_s},#{' ' unless options[:style] == :compressed}#{other.to_s}")
end

- deep_copy

See Also:



37
38
39
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 37

def deep_copy
  dup
end

- (Script::String) div(other)

The SassScript / operation.

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Script::String) — A string containing both literals separated by "/"


146
147
148
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 146

def div(other)
  Sass::Script::String.new("#{self.to_s}/#{other.to_s}")
end

- (Bool) eq(other)

The SassScript == operation. Note that this returns a Sass::Script::Bool object, not a Ruby boolean.

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Bool) — True if this literal is the same as the other, false otherwise


66
67
68
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 66

def eq(other)
  Sass::Script::Bool.new(self.class == other.class && self.value == other.value)
end

- (String) inspect

A readable representation of the literal

Returns:

  • (String) — A readable representation of the literal


178
179
180
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 178

def inspect
  value.inspect
end

- (Script::String) minus(other)

The SassScript - operation.

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Script::String) — A string containing both literals separated by "-"


137
138
139
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 137

def minus(other)
  Sass::Script::String.new("#{self.to_s}-#{other.to_s}")
end

- (Bool) neq(other)

The SassScript != operation. Note that this returns a Sass::Script::Bool object, not a Ruby boolean.

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Bool) — False if this literal is the same as the other, true otherwise


77
78
79
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 77

def neq(other)
  Sass::Script::Bool.new(!eq(other).to_bool)
end

- ({Symbol => Object}) options

Returns the options hash for this node.

Returns:

  • ({Symbol => Object})

Raises:

  • (Sass::SyntaxError) — if the options hash hasn’t been set. This should only happen when the literal was created outside of the parser and #to_s was called on it


47
48
49
50
51
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 47

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

- (Script::String) plus(other)

The SassScript + operation.

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Script::String) — A string containing both literals without any separation


125
126
127
128
129
130
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 125

def plus(other)
  if other.is_a?(Sass::Script::String)
    return Sass::Script::String.new(self.to_s + other.value, other.type)
  end
  Sass::Script::String.new(self.to_s + other.to_s)
end

- (Script::String) single_eq(other)

The SassScript = operation (used for proprietary MS syntax like alpha(opacity=20)).

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Script::String) — A string containing both literals separated by "="


116
117
118
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 116

def single_eq(other)
  Sass::Script::String.new("#{self.to_s}=#{other.to_s}")
end

- (Script::String) space(other)

The SassScript default operation (e.g. $a $b, "foo" "bar").

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Script::String) — A string containing both literals separated by a space


97
98
99
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 97

def space(other)
  Sass::Script::String.new("#{self.to_s} #{other.to_s}")
end

- (Array<Literal>) to_a

Returns the value of this literal as a list. Single literals are considered the same as single-element lists.

Returns:

  • (Array<Literal>) — The of this literal as a list


208
209
210
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 208

def to_a
  [self]
end

- (Boolean) to_bool

true (the Ruby boolean value)

Returns:

  • (Boolean)true (the Ruby boolean value)


183
184
185
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 183

def to_bool
  true
end

- (Fixnum) to_i

The integer value of this literal

Returns:

  • (Fixnum) — The integer value of this literal

Raises:



197
198
199
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 197

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

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

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

Returns:

Raises:



216
217
218
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 216

def to_s(opts = {})
  raise Sass::SyntaxError.new("[BUG] All subclasses of Sass::Literal must implement #to_s.")
end

- (Script::String) unary_div

The SassScript unary / operation (e.g. /$a).

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Script::String) — A string containing the literal preceded by "/"


173
174
175
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 173

def unary_div
  Sass::Script::String.new("/#{self.to_s}")
end

- (Script::String) unary_minus

The SassScript unary - operation (e.g. -$a).

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Script::String) — A string containing the literal preceded by "-"


164
165
166
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 164

def unary_minus
  Sass::Script::String.new("-#{self.to_s}")
end

- (Bool) unary_not

The SassScript == operation. Note that this returns a Sass::Script::Bool object, not a Ruby boolean.

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Bool) — True if this literal is the same as the other, false otherwise


88
89
90
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 88

def unary_not
  Sass::Script::Bool.new(!to_bool)
end

- (Script::String) unary_plus

The SassScript unary + operation (e.g. +$a).

Parameters:

  • (Literal) other — The right-hand side of the operator

Returns:

  • (Script::String) — A string containing the literal preceded by "+"


155
156
157
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 155

def unary_plus
  Sass::Script::String.new("+#{self.to_s}")
end