Skip to content

Commit

Permalink
Merge pull request #1 from tkawa/master
Browse files Browse the repository at this point in the history
Implement some visitors
  • Loading branch information
amatsuda committed Sep 11, 2013
2 parents 404af12 + b31258e commit 170b961
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions lib/arel/visitors/ruby.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) } } }
Expand All @@ -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
Expand Down

0 comments on commit 170b961

Please sign in to comment.