Class: Sass::Script::Value::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/sass/script/value/base.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, Null, Number, String

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = nil) ⇒ Base

Creates a new value.

Parameters:

  • value (Object) (defaults to: nil)

    The object for #value



22
23
24
25
# File 'lib/sass/script/value/base.rb', line 22

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

Instance Attribute Details

#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 value was created outside of the parser and #to_s was called on it



40
41
42
43
44
45
46
47
48
# File 'lib/sass/script/value/base.rb', line 40

def options
  return @options if @options
  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 value within a custom Sass function without first
  setting the #options attribute.
MSG
end

#source_rangeSass::Source::Range

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

Returns:



17
18
19
# File 'lib/sass/script/value/base.rb', line 17

def source_range
  @source_range
end

#valueObject (readonly)

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

Returns:

  • (Object)


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

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 value is equivalent to other



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

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

#_perform(environment) ⇒ Value (protected)

Evaluates the value.

Parameters:

  • environment (Sass::Environment)

    The environment in which to evaluate the SassScript

Returns:



236
237
238
# File 'lib/sass/script/value/base.rb', line 236

def _perform(environment)
  self
end

#assert_int!

Raises:



185
# File 'lib/sass/script/value/base.rb', line 185

def assert_int!; to_i; end

#div(other) ⇒ Script::Value::String

The SassScript / operation.

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:



117
118
119
# File 'lib/sass/script/value/base.rb', line 117

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

#eq(other) ⇒ Sass::Script::Value::Bool

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

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:



57
58
59
# File 'lib/sass/script/value/base.rb', line 57

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


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

def eql?(other)
  self == other
end

#hashFixnum

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

Returns:

  • (Fixnum)

    The hash code.



152
153
154
# File 'lib/sass/script/value/base.rb', line 152

def hash
  value.hash
end

#inspectString

Returns A readable representation of the value.

Returns:

  • (String)

    A readable representation of the value



161
162
163
# File 'lib/sass/script/value/base.rb', line 161

def inspect
  value.inspect
end

#minus(other) ⇒ Script::Value::String

The SassScript - operation.

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:



108
109
110
# File 'lib/sass/script/value/base.rb', line 108

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

#neq(other) ⇒ Sass::Script::Value::Bool

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

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:



68
69
70
# File 'lib/sass/script/value/base.rb', line 68

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

#null?Boolean

Returns whether or not this object is null.

Returns:

  • (Boolean)

    false



226
227
228
# File 'lib/sass/script/value/base.rb', line 226

def null?
  false
end

#plus(other) ⇒ Script::Value::String

The SassScript + operation.

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:



98
99
100
101
# File 'lib/sass/script/value/base.rb', line 98

def plus(other)
  type = other.is_a?(Sass::Script::Value::String) ? other.type : :identifier
  Sass::Script::Value::String.new(to_s(:quote => :none) + other.to_s(:quote => :none), type)
end

#separatorSymbol

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.

Returns:

  • (Symbol)


192
# File 'lib/sass/script/value/base.rb', line 192

def separator; nil; end

#single_eq(other) ⇒ Script::Value::String

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

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:



89
90
91
# File 'lib/sass/script/value/base.rb', line 89

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

#to_aArray<Value>

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

Returns:

  • (Array<Value>)

    This value as a list



198
199
200
# File 'lib/sass/script/value/base.rb', line 198

def to_a
  [self]
end

#to_boolBoolean

Returns true (the Ruby boolean value).

Returns:

  • (Boolean)

    true (the Ruby boolean value)



166
167
168
# File 'lib/sass/script/value/base.rb', line 166

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:



207
208
209
# File 'lib/sass/script/value/base.rb', line 207

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

#to_iFixnum

Returns The integer value of this value.

Returns:

  • (Fixnum)

    The integer value of this value

Raises:



180
181
182
# File 'lib/sass/script/value/base.rb', line 180

def to_i
  raise Sass::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:



218
219
220
# File 'lib/sass/script/value/base.rb', line 218

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

#unary_divScript::Value::String

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

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:



144
145
146
# File 'lib/sass/script/value/base.rb', line 144

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

#unary_minusScript::Value::String

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

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:



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

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

#unary_notSass::Script::Value::Bool

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

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:



79
80
81
# File 'lib/sass/script/value/base.rb', line 79

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

#unary_plusScript::Value::String

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

Parameters:

  • other (Value)

    The right-hand side of the operator

Returns:



126
127
128
# File 'lib/sass/script/value/base.rb', line 126

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