Browse files

Implement Nary#hash, ensure internal comparisons are done via eql?

Fixes #38
  • Loading branch information...
1 parent 06537df commit 72e6f9f262b32437482412ebc156d8b55006fc3b @ernie ernie committed Aug 8, 2011
View
6 lib/squeel/nodes/binary.rb
@@ -21,9 +21,9 @@ def initialize(left, right)
# Comparison with other nodes
def eql?(other)
- self.class == other.class &&
- self.left == other.left &&
- self.right == other.right
+ self.class.eql?(other.class) &&
+ self.left.eql?(other.left) &&
+ self.right.eql?(other.right)
end
alias :== :eql?
View
8 lib/squeel/nodes/join.rb
@@ -52,10 +52,10 @@ def polymorphic?
# Compare with other objects
def eql?(other)
- self.class == other.class &&
- self._name == other._name &&
- self._type == other._type &&
- self._klass == other._klass
+ self.class.eql?(other.class) &&
+ self._name.eql?(other._name) &&
+ self._type.eql?(other._type) &&
+ self._klass.eql?(other._klass)
end
alias :== :eql?
View
7 lib/squeel/nodes/key_path.rb
@@ -42,10 +42,10 @@ def absolute?
# Object comparison
def eql?(other)
- self.class == other.class &&
- self.path == other.path &&
+ self.class.eql?(other.class) &&
+ self.path.eql?(other.path) &&
self.endpoint.eql?(other.endpoint) &&
- self.absolute? == other.absolute?
+ self.absolute?.eql?(other.absolute?)
end
# Allow KeyPath to function like its endpoint, in the case where its endpoint
@@ -161,6 +161,7 @@ def to_s
# @return [KeyPath] The updated KeyPath
def method_missing(method_id, *args)
super if method_id == :to_ary
+
if endpoint.respond_to? method_id
@endpoint = @endpoint.send(method_id, *args)
self
View
4 lib/squeel/nodes/literal.rb
@@ -42,8 +42,8 @@ def desc
# Object comparison
def eql?(other)
- self.class == other.class &&
- self.expr == other.expr
+ self.class.eql?(other.class) &&
+ self.expr.eql?(other.expr)
end
# To support object equality tests
View
9 lib/squeel/nodes/nary.rb
@@ -33,10 +33,15 @@ def -(other)
self
end
+ # Implemented for equality testing
+ def hash
+ [self.class].concat(self.children).hash
+ end
+
# Object comparison
def eql?(other)
- self.class == other.class &&
- self.children == other.children
+ self.class.eql?(other.class) &&
+ self.children.eql?(other.children)
end
alias :== :eql?
View
4 lib/squeel/nodes/stub.rb
@@ -37,8 +37,8 @@ def initialize(symbol)
# Object comparison
def eql?(other)
- self.class == other.class &&
- self.symbol == other.symbol
+ self.class.eql?(other.class) &&
+ self.symbol.eql?(other.symbol)
end
# To support object equality tests
View
4 lib/squeel/nodes/unary.rb
@@ -18,8 +18,8 @@ def initialize(expr)
# Object comparison
def eql?(other)
- self.class == other.class &&
- self.expr == other.expr
+ self.class.eql?(other.class) &&
+ self.expr.eql?(other.expr)
end
alias :== :eql?
View
2 lib/squeel/version.rb
@@ -1,3 +1,3 @@
module Squeel
- VERSION = "0.8.6"
+ VERSION = "0.8.7"
end
View
9 spec/squeel/adapters/active_record/relation_spec.rb
@@ -189,6 +189,15 @@ module ActiveRecord
arel.to_sql.should match /ORDER BY "parents_people_2"."id" ASC/
end
+ it 'does not inadvertently convert KeyPaths to booleans when uniqing where_values' do
+ 100.times do # Doesn't happen reliably because of #hash behavior
+ persons = Person.joins{[outgoing_messages.outer, incoming_messages.outer]}
+ persons = persons.where { (outgoing_messages.author_id.not_eq 7) & (incoming_messages.author_id.not_eq 7) }
+ persons = persons.where{(outgoing_messages.recipient_id.not_eq 7) & (incoming_messages.recipient_id.not_eq 7)}
+ expect { persons.to_sql }.not_to raise_error TypeError
+ end
+ end
+
end
describe '#to_sql' do
View
14 spec/squeel/visitors/predicate_visitor_spec.rb
@@ -367,20 +367,6 @@ module Visitors
end
end
- context 'with multiple joins to the same table' do
-
- it 'should not raise an exception' do
- expect do
- 100.times do
- persons = Person.joins{[outgoing_messages.outer, incoming_messages.outer]}
- persons = persons.where { (outgoing_messages.author_id.not_eq 7) & (incoming_messages.author_id.not_eq 7) }
- persons.where{(outgoing_messages.recipient_id.not_eq 7) & (incoming_messages.recipient_id.not_eq 7)}.to_sql
- end
- end.to_not raise_exception
- end
-
- end
-
end
end
end

0 comments on commit 72e6f9f

Please sign in to comment.