aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/visitors/bind_visitor.rb
blob: c336e87395af305426bd362dc05dd46194312abd (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
module Arel
  module Visitors
    module BindVisitor
      def initialize target
        @block = nil
        super
      end

      def accept node, collector, &block
        @block = block if block_given?
        super
      end

      private

      def visit_Arel_Nodes_Assignment o, collector
        if o.right.is_a? Arel::Nodes::BindParam
          collector = visit o.left, collector
          collector << " = "
          visit o.right, collector
        else
          super
        end
      end

      def visit_Arel_Nodes_BindParam o, collector
        if @block
          val = @block.call
          if String === val
            collector << val
          end
        else
          super
        end
      end

    end
  end
end