Skip to content

Commit

Permalink
Merge pull request #490 from MaxLap/fix_nodes_hash_eql_eqeq
Browse files Browse the repository at this point in the history
Add missing hash, eql?, == to various node classes
  • Loading branch information
sgrif committed Jul 25, 2017
2 parents 0e7ce3f + 6d225a9 commit a4154ea
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 1 deletion.
7 changes: 6 additions & 1 deletion lib/arel/nodes/bind_param.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ def initialize(value)
super()
end

def ==(other)
def hash
[self.class, self.value].hash
end

def eql?(other)
other.is_a?(BindParam) &&
value == other.value
end
alias :== :eql?

def nil?
value.nil?
Expand Down
1 change: 1 addition & 0 deletions lib/arel/nodes/false.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def hash
def eql? other
self.class == other.class
end
alias :== :eql?
end
end
end
2 changes: 2 additions & 0 deletions lib/arel/nodes/function.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def eql? other
self.alias == other.alias &&
self.distinct == other.distinct
end
alias :== :eql?

end

%w{
Expand Down
1 change: 1 addition & 0 deletions lib/arel/nodes/terminal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def hash
def eql? other
self.class == other.class
end
alias :== :eql?
end
end
end
1 change: 1 addition & 0 deletions lib/arel/nodes/true.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ def hash
def eql? other
self.class == other.class
end
alias :== :eql?
end
end
end
10 changes: 10 additions & 0 deletions lib/arel/nodes/values_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,16 @@ def initialize(rows)
@rows = rows
super()
end

def hash
@rows.hash
end

def eql? other
self.class == other.class &&
self.rows == other.rows
end
alias :== :eql?
end
end
end
1 change: 1 addition & 0 deletions lib/arel/nodes/window.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ def hash
def eql? other
self.class == other.class
end
alias :== :eql?
end

class Preceding < Unary
Expand Down
34 changes: 34 additions & 0 deletions test/test_nodes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true
require 'helper'

module Arel
module Nodes
class TestNodes < Minitest::Test
def test_every_arel_nodes_have_hash_eql_eqeq_from_same_class
# #descendants code from activesupport
node_descendants = []
ObjectSpace.each_object(Arel::Nodes::Node.singleton_class) do |k|
next if k.respond_to?(:singleton_class?) && k.singleton_class?
node_descendants.unshift k unless k == self
end
node_descendants.delete(Arel::Nodes::Node)

bad_node_descendants = node_descendants.reject do |subnode|
eqeq_owner = subnode.instance_method(:==).owner
eql_owner = subnode.instance_method(:eql?).owner
hash_owner = subnode.instance_method(:hash).owner

eqeq_owner < Arel::Nodes::Node &&
eqeq_owner == eql_owner &&
eqeq_owner == hash_owner
end

problem_msg = 'Some subclasses of Arel::Nodes::Node do not have a' \
' #== or #eql? or #hash defined from the same class as the others'
assert_empty bad_node_descendants, problem_msg
end


end
end
end

0 comments on commit a4154ea

Please sign in to comment.