Sha256: b2b1254c0b0ff83273dcf23a6dfd436ccd7c04a5212073c6b295501ea784fd33

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# encoding: utf-8

module Piglet
  module Field
    class InfixExpression # :nodoc:
      include Field
      
      attr_reader :operator
    
      def initialize(operator, left_expression, right_expression, options=nil)
        options ||= {}
        @operator, @left_expression, @right_expression = operator, left_expression, right_expression
        if options[:type]
          @type = options[:type]
        else
          @type = determine_type(@left_expression, @right_expression)
        end
        @predecessors = [left_expression, right_expression]
      end
    
      def simple?
        false
      end
    
      def to_s(inner=false)
        if inner
          left  = @left_expression.field_alias
          right = @right_expression.field_alias
        else
          left  = @left_expression
          right = @right_expression

          if left.respond_to?(:operator) && left.operator != @operator
            left = parenthesise(left)
          end
        
          if right.respond_to?(:operator) && right.operator != @operator
            right = parenthesise(right)
          end
        end
        "#{left} #{@operator} #{right}"
      end
      
    private
    
      def determine_type(left, right)
        left_type = expression_type(left)
        right_type = expression_type(right)

        if left_type == :double || right_type == :double
          :double
        elsif left_type == :float || right_type == :float
          :float
        elsif left_type == :long || right_type == :long
          :long
        else
          left_type
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
piglet-0.3.0 lib/piglet/field/infix_expression.rb