Permalink
Browse files

Create Predicates module to match directory structure

  • Loading branch information...
brynary committed Oct 1, 2009
1 parent 52e8aff commit bcd8ffa2183a1ca98417fb39bbd83e8f69c984c8
@@ -1,41 +1,43 @@
module Arel
- class Predicate
- def or(other_predicate)
- Or.new(self, other_predicate)
- end
+ module Predicates
+ class Predicate
+ def or(other_predicate)
+ Or.new(self, other_predicate)
+ end
- def and(other_predicate)
- And.new(self, other_predicate)
+ def and(other_predicate)
+ And.new(self, other_predicate)
+ end
end
- end
- class Binary < Predicate
- attributes :operand1, :operand2
- deriving :initialize
+ class Binary < Predicate
+ attributes :operand1, :operand2
+ deriving :initialize
- def ==(other)
- self.class === other and
- @operand1 == other.operand1 and
- @operand2 == other.operand2
- end
+ def ==(other)
+ self.class === other and
+ @operand1 == other.operand1 and
+ @operand2 == other.operand2
+ end
- def bind(relation)
- self.class.new(operand1.find_correlate_in(relation), operand2.find_correlate_in(relation))
+ def bind(relation)
+ self.class.new(operand1.find_correlate_in(relation), operand2.find_correlate_in(relation))
+ end
end
- end
- class Equality < Binary
- def ==(other)
- Equality === other and
- ((operand1 == other.operand1 and operand2 == other.operand2) or
- (operand1 == other.operand2 and operand2 == other.operand1))
+ class Equality < Binary
+ def ==(other)
+ Equality === other and
+ ((operand1 == other.operand1 and operand2 == other.operand2) or
+ (operand1 == other.operand2 and operand2 == other.operand1))
+ end
end
- end
- class GreaterThanOrEqualTo < Binary; end
- class GreaterThan < Binary; end
- class LessThanOrEqualTo < Binary; end
- class LessThan < Binary; end
- class Match < Binary; end
- class In < Binary; end
+ class GreaterThanOrEqualTo < Binary; end
+ class GreaterThan < Binary; end
+ class LessThanOrEqualTo < Binary; end
+ class LessThan < Binary; end
+ class Match < Binary; end
+ class In < Binary; end
+ end
end
@@ -82,31 +82,31 @@ def /(other)
module Predications
def eq(other)
- Equality.new(self, other)
+ Predicates::Equality.new(self, other)
end
def lt(other)
- LessThan.new(self, other)
+ Predicates::LessThan.new(self, other)
end
def lteq(other)
- LessThanOrEqualTo.new(self, other)
+ Predicates::LessThanOrEqualTo.new(self, other)
end
def gt(other)
- GreaterThan.new(self, other)
+ Predicates::GreaterThan.new(self, other)
end
def gteq(other)
- GreaterThanOrEqualTo.new(self, other)
+ Predicates::GreaterThanOrEqualTo.new(self, other)
end
def matches(regexp)
- Match.new(self, regexp)
+ Predicates::Match.new(self, regexp)
end
def in(array)
- In.new(self, array)
+ Predicates::In.new(self, array)
end
end
include Predications
@@ -1,35 +1,37 @@
module Arel
- class Binary < Predicate
- def eval(row)
- operand1.eval(row).send(operator, operand2.eval(row))
+ module Predicates
+ class Binary < Predicate
+ def eval(row)
+ operand1.eval(row).send(operator, operand2.eval(row))
+ end
end
- end
- class Equality < Binary
- def operator; :== end
- end
+ class Equality < Binary
+ def operator; :== end
+ end
- class GreaterThanOrEqualTo < Binary
- def operator; :>= end
- end
+ class GreaterThanOrEqualTo < Binary
+ def operator; :>= end
+ end
- class GreaterThan < Binary
- def operator; :> end
- end
+ class GreaterThan < Binary
+ def operator; :> end
+ end
- class LessThanOrEqualTo < Binary
- def operator; :<= end
- end
+ class LessThanOrEqualTo < Binary
+ def operator; :<= end
+ end
- class LessThan < Binary
- def operator; :< end
- end
+ class LessThan < Binary
+ def operator; :< end
+ end
- class Match < Binary
- def operator; :=~ end
- end
+ class Match < Binary
+ def operator; :=~ end
+ end
- class In < Binary
- def operator; :include? end
+ class In < Binary
+ def operator; :include? end
+ end
end
end
@@ -1,51 +1,53 @@
module Arel
- class Binary < Predicate
- def to_sql(formatter = nil)
- "#{operand1.to_sql} #{predicate_sql} #{operand1.format(operand2)}"
+ module Predicates
+ class Binary < Predicate
+ def to_sql(formatter = nil)
+ "#{operand1.to_sql} #{predicate_sql} #{operand1.format(operand2)}"
+ end
end
- end
- class CompoundPredicate < Binary
- def to_sql(formatter = nil)
- "(#{operand1.to_sql(formatter)} #{predicate_sql} #{operand2.to_sql(formatter)})"
+ class CompoundPredicate < Binary
+ def to_sql(formatter = nil)
+ "(#{operand1.to_sql(formatter)} #{predicate_sql} #{operand2.to_sql(formatter)})"
+ end
end
- end
- class Or < CompoundPredicate
- def predicate_sql; "OR" end
- end
+ class Or < CompoundPredicate
+ def predicate_sql; "OR" end
+ end
- class And < CompoundPredicate
- def predicate_sql; "AND" end
- end
+ class And < CompoundPredicate
+ def predicate_sql; "AND" end
+ end
- class Equality < Binary
- def predicate_sql
- operand2.equality_predicate_sql
+ class Equality < Binary
+ def predicate_sql
+ operand2.equality_predicate_sql
+ end
end
- end
- class GreaterThanOrEqualTo < Binary
- def predicate_sql; '>=' end
- end
+ class GreaterThanOrEqualTo < Binary
+ def predicate_sql; '>=' end
+ end
- class GreaterThan < Binary
- def predicate_sql; '>' end
- end
+ class GreaterThan < Binary
+ def predicate_sql; '>' end
+ end
- class LessThanOrEqualTo < Binary
- def predicate_sql; '<=' end
- end
+ class LessThanOrEqualTo < Binary
+ def predicate_sql; '<=' end
+ end
- class LessThan < Binary
- def predicate_sql; '<' end
- end
+ class LessThan < Binary
+ def predicate_sql; '<' end
+ end
- class Match < Binary
- def predicate_sql; 'LIKE' end
- end
+ class Match < Binary
+ def predicate_sql; 'LIKE' end
+ end
- class In < Binary
- def predicate_sql; operand2.inclusion_predicate_sql end
+ class In < Binary
+ def predicate_sql; operand2.inclusion_predicate_sql end
+ end
end
end
@@ -1,31 +1,33 @@
require 'spec_helper'
module Arel
- describe Binary do
- before do
- @relation = Table.new(:users)
- @attribute1 = @relation[:id]
- @attribute2 = @relation[:name]
- class ConcreteBinary < Binary
- end
- end
-
- describe '#bind' do
+ module Predicates
+ describe Binary do
before do
- @another_relation = @relation.alias
+ @relation = Table.new(:users)
+ @attribute1 = @relation[:id]
+ @attribute2 = @relation[:name]
+ class ConcreteBinary < Binary
+ end
end
- describe 'when both operands are attributes' do
- it "manufactures an expression with the attributes bound to the relation" do
- ConcreteBinary.new(@attribute1, @attribute2).bind(@another_relation). \
- should == ConcreteBinary.new(@another_relation[@attribute1], @another_relation[@attribute2])
+ describe '#bind' do
+ before do
+ @another_relation = @relation.alias
+ end
+
+ describe 'when both operands are attributes' do
+ it "manufactures an expression with the attributes bound to the relation" do
+ ConcreteBinary.new(@attribute1, @attribute2).bind(@another_relation). \
+ should == ConcreteBinary.new(@another_relation[@attribute1], @another_relation[@attribute2])
+ end
end
- end
- describe 'when an operand is a value' do
- it "manufactures an expression with unmodified values" do
- ConcreteBinary.new(@attribute1, "asdf").bind(@another_relation). \
- should == ConcreteBinary.new(@attribute1.find_correlate_in(@another_relation), "asdf".find_correlate_in(@another_relation))
+ describe 'when an operand is a value' do
+ it "manufactures an expression with unmodified values" do
+ ConcreteBinary.new(@attribute1, "asdf").bind(@another_relation). \
+ should == ConcreteBinary.new(@attribute1.find_correlate_in(@another_relation), "asdf".find_correlate_in(@another_relation))
+ end
end
end
end
@@ -1,26 +1,28 @@
require 'spec_helper'
module Arel
- describe Equality do
- before do
- @relation1 = Table.new(:users)
- @relation2 = Table.new(:photos)
- @attribute1 = @relation1[:id]
- @attribute2 = @relation2[:user_id]
- end
-
- describe '==' do
- it "obtains if attribute1 and attribute2 are identical" do
- check Equality.new(@attribute1, @attribute2).should == Equality.new(@attribute1, @attribute2)
- Equality.new(@attribute1, @attribute2).should_not == Equality.new(@attribute1, @attribute1)
+ module Predicates
+ describe Equality do
+ before do
+ @relation1 = Table.new(:users)
+ @relation2 = Table.new(:photos)
+ @attribute1 = @relation1[:id]
+ @attribute2 = @relation2[:user_id]
end
- it "obtains if the concrete type of the predicates are identical" do
- Equality.new(@attribute1, @attribute2).should_not == Binary.new(@attribute1, @attribute2)
- end
+ describe '==' do
+ it "obtains if attribute1 and attribute2 are identical" do
+ check Equality.new(@attribute1, @attribute2).should == Equality.new(@attribute1, @attribute2)
+ Equality.new(@attribute1, @attribute2).should_not == Equality.new(@attribute1, @attribute1)
+ end
+
+ it "obtains if the concrete type of the predicates are identical" do
+ Equality.new(@attribute1, @attribute2).should_not == Binary.new(@attribute1, @attribute2)
+ end
- it "is commutative on the attributes" do
- Equality.new(@attribute1, @attribute2).should == Equality.new(@attribute2, @attribute1)
+ it "is commutative on the attributes" do
+ Equality.new(@attribute1, @attribute2).should == Equality.new(@attribute2, @attribute1)
+ end
end
end
end
@@ -1,10 +1,12 @@
require 'spec_helper'
module Arel
- describe In do
- before do
- @relation = Table.new(:users)
- @attribute = @relation[:id]
+ module Predicates
+ describe In do
+ before do
+ @relation = Table.new(:users)
+ @attribute = @relation[:id]
+ end
end
end
end
Oops, something went wrong.

0 comments on commit bcd8ffa

Please sign in to comment.