Class: Sass::Script::Literal

Inherits:
Node
  • Object
show all
Defined in:
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 collapse

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#dasherize, #opts, #perform

Constructor Details

#initialize(value = nil) ⇒ Literal

Creates a new literal.

Parameters:

  • value (Object) (defaults to: nil)

    The object for #value



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

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

Instance Attribute Details

#valueObject (readonly)

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

Returns:

  • (Object)


18
19
20
# File 'lib/sass/script/literal.rb', line 18

def value
  @value
end

Instance Method Details

#==(other) ⇒ Boolean

Compares this object with another.

Parameters:

  • other (Object)

    The object to compare with

Returns:

  • (Boolean)

    Whether or not this literal is equivalent to other



210
211
212
# File 'lib/sass/script/literal.rb', line 210

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

#_perform(environment) ⇒ Literal (protected)

Evaluates the literal.

Parameters:

  • environment (Sass::Environment)

    The environment in which to evaluate the SassScript

Returns:



246
247
248
# File 'lib/sass/script/literal.rb', line 246

def _perform(environment)
  self
end

#and(other) ⇒ Literal

The SassScript and operation.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Literal)

    The result of a logical and: other if this literal isn't a false Bool, and this literal otherwise



64
65
66
# File 'lib/sass/script/literal.rb', line 64

def and(other)
  to_bool ? other : self
end

#assert_int!

Raises:



221
# File 'lib/sass/script/literal.rb', line 221

def assert_int!; to_i; end

#childrenArray<Node>

Returns an empty array.

Returns:

  • (Array<Node>)

    empty

See Also:



32
33
34
# File 'lib/sass/script/literal.rb', line 32

def children
  []
end

#comma(other) ⇒ Script::String

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

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Script::String)

    A string containing both literals separated by ", "



125
126
127
# File 'lib/sass/script/literal.rb', line 125

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 'lib/sass/script/literal.rb', line 37

def deep_copy
  dup
end

#div(other) ⇒ Script::String

The SassScript / operation.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Script::String)

    A string containing both literals separated by "/"



165
166
167
# File 'lib/sass/script/literal.rb', line 165

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

#eq(other) ⇒ Bool

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

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Bool)

    True if this literal is the same as the other, false otherwise



85
86
87
# File 'lib/sass/script/literal.rb', line 85

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

#inspectString

Returns A readable representation of the literal.

Returns:

  • (String)

    A readable representation of the literal



197
198
199
# File 'lib/sass/script/literal.rb', line 197

def inspect
  value.inspect
end

#minus(other) ⇒ Script::String

The SassScript - operation.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Script::String)

    A string containing both literals separated by "-"



156
157
158
# File 'lib/sass/script/literal.rb', line 156

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

#neq(other) ⇒ Bool

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

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Bool)

    False if this literal is the same as the other, true otherwise



96
97
98
# File 'lib/sass/script/literal.rb', line 96

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

#options{Symbol => Object}

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
52
53
54
55
56
# File 'lib/sass/script/literal.rb', line 47

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

#or(other) ⇒ Literal

The SassScript or operation.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Literal)

    The result of the logical or: this literal if it isn't a false Bool, and other otherwise



74
75
76
# File 'lib/sass/script/literal.rb', line 74

def or(other)
  to_bool ? self : other
end

#plus(other) ⇒ Script::String

The SassScript + operation.

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Script::String)

    A string containing both literals without any separation



144
145
146
147
148
149
# File 'lib/sass/script/literal.rb', line 144

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

#single_eq(other) ⇒ Script::String

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

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Script::String)

    A string containing both literals separated by "="



135
136
137
# File 'lib/sass/script/literal.rb', line 135

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

#space(other) ⇒ Script::String

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

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Script::String)

    A string containing both literals separated by a space



116
117
118
# File 'lib/sass/script/literal.rb', line 116

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

#to_aArray<Literal>

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



227
228
229
# File 'lib/sass/script/literal.rb', line 227

def to_a
  [self]
end

#to_boolBoolean

Returns true (the Ruby boolean value).

Returns:

  • (Boolean)

    true (the Ruby boolean value)



202
203
204
# File 'lib/sass/script/literal.rb', line 202

def to_bool
  true
end

#to_iFixnum

Returns The integer value of this literal.

Returns:

  • (Fixnum)

    The integer value of this literal

Raises:



216
217
218
# File 'lib/sass/script/literal.rb', line 216

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

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

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

Returns:

Raises:



235
236
237
# File 'lib/sass/script/literal.rb', line 235

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

#unary_divScript::String

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

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Script::String)

    A string containing the literal preceded by "/"



192
193
194
# File 'lib/sass/script/literal.rb', line 192

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

#unary_minusScript::String

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

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Script::String)

    A string containing the literal preceded by "-"



183
184
185
# File 'lib/sass/script/literal.rb', line 183

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

#unary_notBool

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

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Bool)

    True if this literal is the same as the other, false otherwise



107
108
109
# File 'lib/sass/script/literal.rb', line 107

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

#unary_plusScript::String

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

Parameters:

  • other (Literal)

    The right-hand side of the operator

Returns:

  • (Script::String)

    A string containing the literal preceded by "+"



174
175
176
# File 'lib/sass/script/literal.rb', line 174

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