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, Null, 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


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

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)


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

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


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

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:



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

def _perform(environment)
  self
end

- assert_int!

Raises:



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

def assert_int!; to_i end

- (Array<Node>) children

Returns an empty array.

Returns:

  • (Array<Node>) — empty

See Also:



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

def children
  []
end

- deep_copy

See Also:



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

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 "/"


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

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


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

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


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

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 "-"


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

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


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

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

- (Boolean) null?

Returns whether or not this object is null.

Returns:

  • (Boolean)false


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

def null?
  false
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


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

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


109
110
111
112
113
114
# File '/var/www/sass-pages/.sass/lib/sass/script/literal.rb', line 109

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 "="


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

def single_eq(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


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

def to_a
  [self]
end

- (Boolean) to_bool

true (the Ruby boolean value)

Returns:

  • (Boolean)true (the Ruby boolean value)


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

def to_bool
  true
end

- (Fixnum) to_i

The integer value of this literal

Returns:

  • (Fixnum) — The integer value of this literal

Raises:



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

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:



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

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 "/"


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

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 "-"


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

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


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

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 "+"


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

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