From b31258e3d7a979f575fe7d5a796410f20091e7d1 Mon Sep 17 00:00:00 2001 From: Toru KAWAMURA Date: Tue, 10 Sep 2013 19:54:14 +0900 Subject: [PATCH] Implement some visitors * visit_Arel_Nodes_Grouping * visit_Arel_Nodes_Between * visit_Arel_Nodes_Not * visit_Arel_Nodes_In * visit_Arel_Nodes_NotIn * visit_Arel_Nodes_NotEqual --- lib/arel/visitors/ruby.rb | 47 ++++++++++++++++++++++++++++----------- 1 file changed, 34 insertions(+), 13 deletions(-) diff --git a/lib/arel/visitors/ruby.rb b/lib/arel/visitors/ruby.rb index 66da2ea..90a1238 100644 --- a/lib/arel/visitors/ruby.rb +++ b/lib/arel/visitors/ruby.rb @@ -130,8 +130,11 @@ def visit_Arel_Nodes_Limit o ProcWithSource.new("take(#{v})") {|collection| collection.take(v) } end -# def visit_Arel_Nodes_Grouping o -# end + def visit_Arel_Nodes_Grouping o + # doubtful implementation + v = visit o.expr + ProcWithSource.new("select {|g| g.#{v.to_source}}") { |collection| collection.select {|obj| v.call(obj) } } + end # def visit_Arel_Nodes_Ascending o # end @@ -168,8 +171,10 @@ def visit_Arel_Nodes_Group o # def visit_Arel_Nodes_TableAlias o # end -# def visit_Arel_Nodes_Between o -# end + def visit_Arel_Nodes_Between o + l, r = visit(o.left), Range.new(o.right.children.first, o.right.children.last) + ProcWithSource.new("#{l}.in? #{r.inspect}") { |o| o.send(l).in?(r) } + end # def visit_Arel_Nodes_GreaterThanOrEqual o # end @@ -205,17 +210,31 @@ def visit_Arel_Nodes_JoinSource o # def visit_Arel_Nodes_On o # end -# def visit_Arel_Nodes_Not o -# end + def visit_Arel_Nodes_Not o + case o.expr + when Arel::Nodes::Between + c = o.expr + l, r = visit(c.left), Range.new(c.right.children.first, c.right.children.last) + # FIXME: 'not_in?' does not actually exist + ProcWithSource.new("#{l}.not_in? #{r.inspect}") { |o| !o.send(l).in?(r) } + else + raise NotImplementedError, 'general Arel::Nodes::Not not implemented' + end + end # def visit_Arel_Table o # end -# def visit_Arel_Nodes_In o -# end + def visit_Arel_Nodes_In o + l, r = visit(o.left), visit(o.right) + ProcWithSource.new("#{l}.in? #{r.inspect}") { |o| o.send(l).in?(r) } + end -# def visit_Arel_Nodes_NotIn o -# end + def visit_Arel_Nodes_NotIn o + l, r = visit(o.left), visit(o.right) + # FIXME: 'not_in?' does not actually exist + ProcWithSource.new("#{l}.not_in? #{r.inspect}") { |o| !o.send(l).in?(r) } + end def visit_Arel_Nodes_And o o.children.map { |x| ProcWithSource.new("select {|o| o.#{visit(x).to_source}}") { |collection| collection.select {|obj| visit(x).call(obj) } } } @@ -229,11 +248,13 @@ def visit_Arel_Nodes_And o def visit_Arel_Nodes_Equality o l, r = visit(o.left), visit(o.right) - ProcWithSource.new("#{l} == #{r}") { |o| o.send(l) == r } + ProcWithSource.new("#{l} == #{r.inspect}") { |o| o.send(l) == r } end -# def visit_Arel_Nodes_NotEqual o -# end + def visit_Arel_Nodes_NotEqual o + l, r = visit(o.left), visit(o.right) + ProcWithSource.new("#{l} != #{r.inspect}") { |o| o.send(l) != r } + end # def visit_Arel_Nodes_As o # end