Permalink
Browse files

Adding More Network Functions to Arel

Adding Arel visitors, and predicates for:
 - contained_within_or_equals (<<=)
 - contains (>>)
 - contains_or_equals (>>=)
  • Loading branch information...
1 parent 68a8f94 commit fb01a7e1156535266aec8c35b49d91280552e829 @doon doon committed Jan 29, 2013
@@ -5,6 +5,18 @@ class ContainedWithin < Arel::Nodes::Binary
def operator; :<< end
end
+ class ContainedWithinEquals < Arel::Nodes::Binary
+ def operator; '<<='.symbolize end
+ end
+
+ class Contains < Arel::Nodes::Binary
+ def operator; :>> end
+ end
+
+ class ContainsEquals < Arel::Nodes::Binary
+ def operator; '>>='.symbolize end
+ end
+
class ArrayOverlap < Arel::Nodes::Binary
def operator; '&&' end
end
@@ -6,6 +6,18 @@ def contained_within(other)
Nodes::ContainedWithin.new self, other
end
+ def contained_within_or_equals(other)
+ Nodes::ContainedWithinEquals.new self, other
+ end
+
+ def contains(other)
+ Nodes::Contains.new self, other
+ end
+
+ def contains_or_equals(other)
+ Nodes::ContainsEquals.new self, other
+ end
+
def array_overlap(other)
Nodes::ArrayOverlap.new self, other
end
@@ -8,6 +8,18 @@ def visit_Arel_Nodes_ContainedWithin o
"#{visit o.left} << #{visit o.right}"
end
+ def visit_Arel_Nodes_ContainedWithinEquals o
+ "#{visit o.left} <<= #{visit o.right}"
+ end
+
+ def visit_Arel_Nodes_Contains o
+ "#{visit o.left} >> #{visit o.right}"
+ end
+
+ def visit_Arel_Nodes_ContainsEquals o
+ "#{visit o.left} >>= #{visit o.right}"
+ end
+
def visit_Arel_Nodes_ArrayOverlap o
if Array === o.right
right = "{#{o.right.map{|v| change_string(visit(v))}.join(',')}}"
View
@@ -23,11 +23,34 @@ class IpAddress < ActiveRecord::Base
end
end
- describe 'cotained with (<<) operator' do
- it 'converts Arel contained_within statemnts to <<' do
+ describe 'contained with (<<) operator' do
+ it 'converts Arel contained_within statements to <<' do
arel_table = IpAddress.arel_table
arel_table.where(arel_table[:address].contained_within(IPAddr.new('127.0.0.1/24'))).to_sql.should match /<< '127.0.0.0\/24'/
end
end
+
+ describe 'contained within or equals (<<=) operator' do
+ it 'converts Arel contained_within_or_equals statements to <<=' do
+ arel_table = IpAddress.arel_table
+
+ arel_table.where(arel_table[:address].contained_within_or_equals(IPAddr.new('127.0.0.1/24'))).to_sql.should match /<<= '127.0.0.0\/24'/
+ end
+ end
+ describe 'contains (>>) operator' do
+ it 'converts Arel contains statements to >>' do
+ arel_table = IpAddress.arel_table
+
+ arel_table.where(arel_table[:address].contains(IPAddr.new('127.0.0.1/24'))).to_sql.should match />> '127.0.0.0\/24'/
+ end
+ end
+
+ describe 'contains or equals (>>=) operator' do
+ it 'converts Arel contains_or_equals statements to >>=' do
+ arel_table = IpAddress.arel_table
+
+ arel_table.where(arel_table[:address].contains_or_equals(IPAddr.new('127.0.0.1/24'))).to_sql.should match />>= '127.0.0.0\/24'/
+ end
+ end
end

0 comments on commit fb01a7e

Please sign in to comment.