Class: Sass::Script::Tree::UnaryOperation
- Defined in:
- lib/sass/script/tree/unary_operation.rb
Overview
A SassScript parse node representing a unary operation,
such as -$b
or not true
.
Currently only -
, /
, and not
are unary operators.
Instance Attribute Summary collapse
-
#operand ⇒ Script::Node
readonly
The parse-tree node for the object of the operator.
-
#operator ⇒ Symbol
readonly
The operation to perform.
Attributes inherited from Node
#filename, #line, #options, #source_range
Instance Method Summary collapse
-
#_perform(environment) ⇒ Sass::Script::Value
protected
Evaluates the operation.
-
#children ⇒ Array<Node>
Returns the operand of the operation.
- #deep_copy
-
#initialize(operand, operator) ⇒ UnaryOperation
constructor
A new instance of UnaryOperation.
-
#inspect ⇒ String
A human-readable s-expression representation of the operation.
- #to_sass(opts = {})
Methods inherited from Node
#dasherize, #force_division!, #opts, #perform
Constructor Details
#initialize(operand, operator) ⇒ UnaryOperation
Returns a new instance of UnaryOperation.
15 16 17 18 19 |
# File 'lib/sass/script/tree/unary_operation.rb', line 15
def initialize(operand, operator)
@operand = operand
@operator = operator
super()
end
|
Instance Attribute Details
#operand ⇒ Script::Node (readonly)
Returns The parse-tree node for the object of the operator.
11 12 13 |
# File 'lib/sass/script/tree/unary_operation.rb', line 11
def operand
@operand
end
|
#operator ⇒ Symbol (readonly)
Returns The operation to perform.
8 9 10 |
# File 'lib/sass/script/tree/unary_operation.rb', line 8
def operator
@operator
end
|
Instance Method Details
#_perform(environment) ⇒ Sass::Script::Value (protected)
Evaluates the operation.
60 61 62 63 64 65 66 67 |
# File 'lib/sass/script/tree/unary_operation.rb', line 60
def _perform(environment)
operator = "unary_#{@operator}"
value = @operand.perform(environment)
value.send(operator)
rescue NoMethodError => e
raise e unless e.name.to_s == operator.to_s
raise Sass::SyntaxError.new("Undefined unary operation: \"#{@operator} #{value}\".")
end
|
#children ⇒ Array<Node>
Returns the operand of the operation.
42 43 44 |
# File 'lib/sass/script/tree/unary_operation.rb', line 42
def children
[@operand]
end
|
#deep_copy
47 48 49 50 51 |
# File 'lib/sass/script/tree/unary_operation.rb', line 47
def deep_copy
node = dup
node.instance_variable_set('@operand', @operand.deep_copy)
node
end
|
#inspect ⇒ String
Returns A human-readable s-expression representation of the operation.
22 23 24 |
# File 'lib/sass/script/tree/unary_operation.rb', line 22
def inspect
"(#{@operator.inspect} #{@operand.inspect})"
end
|
#to_sass(opts = {})
27 28 29 30 31 32 33 34 35 36 |
# File 'lib/sass/script/tree/unary_operation.rb', line 27
def to_sass(opts = {})
operand = @operand.to_sass(opts)
if @operand.is_a?(Operation) ||
(@operator == :minus &&
(operand =~ Sass::SCSS::RX::IDENT) == 0)
operand = "(#{@operand.to_sass(opts)})"
end
op = Sass::Script::Lexer::OPERATORS_REVERSE[@operator]
op + (op =~ /[a-z]/ ? " " : "") + operand
end
|