forked from rails/arel
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added a module for visiting and transforming bind values
- Loading branch information
1 parent
aa43fe1
commit bb22e84
Showing
3 changed files
with
65 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Arel | ||
module Visitors | ||
module BindVisitor | ||
def initialize target | ||
@block = nil | ||
super | ||
end | ||
|
||
def accept node, &block | ||
@block = block if block_given? | ||
super | ||
end | ||
|
||
private | ||
def visit_Arel_Nodes_BindParam o | ||
if @block | ||
@block.call | ||
else | ||
super | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
require 'helper' | ||
require 'arel/visitors/bind_visitor' | ||
|
||
module Arel | ||
module Visitors | ||
class TestBindVisitor < MiniTest::Unit::TestCase | ||
def test_visitor_yields_on_binds | ||
visitor = Class.new(Arel::Visitors::Visitor) { | ||
def initialize omg | ||
end | ||
|
||
include Arel::Visitors::BindVisitor | ||
}.new nil | ||
|
||
bp = Nodes::BindParam.new 'omg' | ||
called = false | ||
visitor.accept(bp) { called = true } | ||
assert called | ||
end | ||
|
||
def test_visitor_only_yields_on_binds | ||
visitor = Class.new(Arel::Visitors::Visitor) { | ||
def initialize omg | ||
end | ||
|
||
include Arel::Visitors::BindVisitor | ||
}.new(nil) | ||
|
||
bp = Arel.sql 'omg' | ||
called = false | ||
|
||
assert_raises(TypeError) { | ||
visitor.accept(bp) { called = true } | ||
} | ||
refute called | ||
end | ||
end | ||
end | ||
end |