aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/nodes/casted.rb
blob: 9fa02955ef586a5546cd5b6140ee79fa4034acac (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
module Arel
  module Nodes
    class Casted < Arel::Nodes::Node # :nodoc:
      attr_reader :val, :attribute
      def initialize val, attribute
        @val       = val
        @attribute = attribute
        super()
      end

      def nil?; @val.nil?; end

      def eql? other
        self.class == other.class &&
            self.val == other.val &&
            self.attribute == other.attribute
      end
      alias :== :eql?
    end

    class Quoted < Arel::Nodes::Unary # :nodoc:
      alias :val :value
      def nil?; val.nil?; end
    end

    def self.build_quoted other, attribute = nil
      case other
        when Arel::Nodes::Node, Arel::Attributes::Attribute, Arel::Table, Arel::Nodes::BindParam, Arel::SelectManager, Arel::Nodes::Quoted
          other
        else
          case attribute
            when Arel::Attributes::Attribute
              Casted.new other, attribute
            else
              Quoted.new other
          end
      end
    end
  end
end