PageRenderTime 25ms CodeModel.GetById 18ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/scalate-jruby/src/main/resources/haml-3.0.25/lib/sass/script/unary_operation.rb

http://github.com/scalate/scalate
Ruby | 57 lines | 34 code | 5 blank | 18 comment | 4 complexity | b5bb7309afa4bd4f216fcafdcf2eee9b MD5 | raw file
 1module Sass::Script
 2  # A SassScript parse node representing a unary operation,
 3  # such as `-$b` or `not true`.
 4  #
 5  # Currently only `-`, `/`, and `not` are unary operators.
 6  class UnaryOperation < Node
 7    # @param operand [Script::Node] The parse-tree node
 8    #   for the object of the operator
 9    # @param operator [Symbol] The operator to perform
10    def initialize(operand, operator)
11      @operand = operand
12      @operator = operator
13      super()
14    end
15
16    # @return [String] A human-readable s-expression representation of the operation
17    def inspect
18      "(#{@operator.inspect} #{@operand.inspect})"
19    end
20
21    # @see Node#to_sass
22    def to_sass(opts = {})
23      operand = @operand.to_sass(opts)
24      if @operand.is_a?(Operation) ||
25          (@operator == :minus &&
26           (operand =~ Sass::SCSS::RX::IDENT) == 0)
27        operand = "(#{@operand.to_sass(opts)})"
28      end
29      op = Lexer::OPERATORS_REVERSE[@operator]
30      op + (op =~ /[a-z]/ ? " " : "") + operand
31    end
32
33    # Returns the operand of the operation.
34    #
35    # @return [Array<Node>]
36    # @see Node#children
37    def children
38      [@operand]
39    end
40
41    protected
42
43    # Evaluates the operation.
44    #
45    # @param environment [Sass::Environment] The environment in which to evaluate the SassScript
46    # @return [Literal] The SassScript object that is the value of the operation
47    # @raise [Sass::SyntaxError] if the operation is undefined for the operand
48    def _perform(environment)
49      operator = "unary_#{@operator}"
50      literal = @operand.perform(environment)
51      literal.send(operator)
52    rescue NoMethodError => e
53      raise e unless e.name.to_s == operator.to_s
54      raise Sass::SyntaxError.new("Undefined unary operation: \"#{@operator} #{literal}\".")
55    end
56  end
57end