2008-10-12 19:03:06 -07:00
|
|
|
module Sass::Script
|
2009-04-24 20:56:12 -07:00
|
|
|
# A SassScript parse node representing a unary operation,
|
|
|
|
# such as `-!b` or `not true`.
|
|
|
|
#
|
|
|
|
# Currently only `-`, `/`, and `not` are unary operators.
|
2009-04-25 02:00:36 -07:00
|
|
|
class UnaryOperation < Node
|
|
|
|
# @param operand [Script::Node] The parse-tree node
|
2009-04-24 20:56:12 -07:00
|
|
|
# for the object of the operator
|
|
|
|
# @param operator [Symbol] The operator to perform
|
2008-06-15 01:15:59 -07:00
|
|
|
def initialize(operand, operator)
|
|
|
|
@operand = operand
|
|
|
|
@operator = operator
|
|
|
|
end
|
|
|
|
|
2009-04-24 20:56:12 -07:00
|
|
|
# @return [String] A human-readable s-expression representation of the operation
|
2008-06-15 01:15:59 -07:00
|
|
|
def inspect
|
|
|
|
"(#{@operator.inspect} #{@operand.inspect})"
|
|
|
|
end
|
|
|
|
|
2009-04-24 20:56:12 -07:00
|
|
|
# Evaluates the operation.
|
|
|
|
#
|
|
|
|
# @param environment [Sass::Environment] The environment in which to evaluate the SassScript
|
|
|
|
# @return [Literal] The SassScript object that is the value of the operation
|
|
|
|
# @raise [Sass::SyntaxError] if the operation is undefined for the operand
|
2008-10-12 20:26:56 -07:00
|
|
|
def perform(environment)
|
2008-06-15 01:15:59 -07:00
|
|
|
operator = "unary_#{@operator}"
|
2008-10-12 20:26:56 -07:00
|
|
|
literal = @operand.perform(environment)
|
2008-06-15 01:15:59 -07:00
|
|
|
literal.send(operator)
|
|
|
|
rescue NoMethodError => e
|
|
|
|
raise e unless e.name.to_s == operator.to_s
|
|
|
|
raise Sass::SyntaxError.new("Undefined unary operation: \"#{@operator} #{literal}\".")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|