From 2f0a8ae8d4aed502a0b6c4780d4545c147bdcb47 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Wed, 21 Jul 2010 10:34:11 -0700 Subject: fixing incompatibilities with AR --- lib/arel/algebra/predicates.rb | 28 ++++++++++++++++++---------- lib/arel/engines/memory/predicates.rb | 2 +- lib/arel/engines/sql/engine.rb | 3 +-- lib/arel/engines/sql/predicates.rb | 2 +- lib/arel/session.rb | 2 ++ spec/engines/sql/unit/engine_spec.rb | 27 +++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 14 deletions(-) diff --git a/lib/arel/algebra/predicates.rb b/lib/arel/algebra/predicates.rb index 1b9667ac13..b761eab417 100644 --- a/lib/arel/algebra/predicates.rb +++ b/lib/arel/algebra/predicates.rb @@ -1,6 +1,6 @@ module Arel module Predicates - class Predicate < Struct.new(:children) + class Predicate def or(other_predicate) Or.new(self, other_predicate) end @@ -16,17 +16,13 @@ module Arel def not self.complement end - - def == other - super || (self.class === other && children == other.children) - end end class Polyadic < Predicate - alias :predicates :children + attr_reader :predicates def initialize(*predicates) - super(predicates) + @predicates = predicates end # Build a Polyadic predicate based on: @@ -42,6 +38,10 @@ module Arel ) end + def ==(other) + super || predicates == other.predicates + end + def bind(relation) self.class.new( *predicates.map {|p| p.find_correlate_in(relation)} @@ -62,11 +62,19 @@ module Arel end class Unary < Predicate - alias :operand :children + attr_reader :operand + + def initialize operand + @operand = operand + end def bind(relation) self.class.new(operand.find_correlate_in(relation)) end + + def == other + super || self.class === other && operand == other.operand + end end class Not < Unary @@ -75,9 +83,9 @@ module Arel end end - class Binary < Predicate - alias :operand1 :children + class Binary < Unary attr_reader :operand2 + alias :operand1 :operand def initialize left, right super(left) diff --git a/lib/arel/engines/memory/predicates.rb b/lib/arel/engines/memory/predicates.rb index e36afa38c2..1527b04056 100644 --- a/lib/arel/engines/memory/predicates.rb +++ b/lib/arel/engines/memory/predicates.rb @@ -1,6 +1,6 @@ module Arel module Predicates - class Binary < Predicate + class Binary < Unary def eval(row) operand1.eval(row).send(operator, operand2.eval(row)) end diff --git a/lib/arel/engines/sql/engine.rb b/lib/arel/engines/sql/engine.rb index 07940c6ef1..a314a972c0 100644 --- a/lib/arel/engines/sql/engine.rb +++ b/lib/arel/engines/sql/engine.rb @@ -4,11 +4,10 @@ module Arel def initialize(ar = nil) @ar = ar - @connection = nil end def connection - @connection ||= @ar && @ar.connection + @ar ? @ar.connection : nil end def adapter_name diff --git a/lib/arel/engines/sql/predicates.rb b/lib/arel/engines/sql/predicates.rb index 21d8f840c9..74a36d77c9 100644 --- a/lib/arel/engines/sql/predicates.rb +++ b/lib/arel/engines/sql/predicates.rb @@ -1,6 +1,6 @@ module Arel module Predicates - class Binary < Predicate + class Binary < Unary def to_sql(formatter = nil) "#{operand1.to_sql} #{predicate_sql} #{operand1.format(operand2)}" end diff --git a/lib/arel/session.rb b/lib/arel/session.rb index 07809e53ba..f6016431c3 100644 --- a/lib/arel/session.rb +++ b/lib/arel/session.rb @@ -1,5 +1,7 @@ module Arel class Session + @instance = nil + def self.instance @instance || new end diff --git a/spec/engines/sql/unit/engine_spec.rb b/spec/engines/sql/unit/engine_spec.rb index f782f56938..85a9dc5bfb 100644 --- a/spec/engines/sql/unit/engine_spec.rb +++ b/spec/engines/sql/unit/engine_spec.rb @@ -1,12 +1,39 @@ require 'spec_helper' module Arel + FakeAR = Struct.new(:connection) + class FakeConnection < Struct.new :called + def initialize c = []; super; end + + def method_missing name, *args, &block + called << [name, args, block] + end + end + describe Sql::Engine do before do @users = Table.new(:users) @users.delete end + describe "method missing" do + it "should pass through" do + conn = FakeConnection.new + engine = Arel::Sql::Engine.new FakeAR.new conn + engine.foo + conn.called.should == [[:foo, [], nil]] + end + + it "should ask for a connection" do + conn = FakeConnection.new + ar = FakeAR.new conn + engine = Arel::Sql::Engine.new ar + + ar.connection = nil + lambda { engine.foo }.should raise_error + end + end + describe "CRUD" do describe "#create" do it "inserts into the relation" do -- cgit v1.2.3