Skip to content

Commit

Permalink
ruby hash comparison starts with comparing hash values
Browse files Browse the repository at this point in the history
  • Loading branch information
bjornblomqvist committed Apr 24, 2013
1 parent 6fd55a6 commit 208c7c9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/plucky/extensions/symbol.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ def <=>(other)
field.to_s <=> other.field.to_s field.to_s <=> other.field.to_s
end end
end end

def hash
field.hash + operator.hash
end


def ==(other) def ==(other)
other.class == self.class && field == other.field && operator == other.operator other.class == self.class && field == other.field && operator == other.operator
Expand Down
10 changes: 9 additions & 1 deletion spec/symbol_operator_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,7 +34,15 @@
SymbolOperator.new(:foo, 'in').should_not == 'foo.in' SymbolOperator.new(:foo, 'in').should_not == 'foo.in'
end end
end end


context "hash" do

it 'returns sum of operator and hash field' do
SymbolOperator.new(:foo, 'in').hash.should == :foo.hash + 'in'.hash
end

end

context "<=>" do context "<=>" do
it "returns string comparison of operator for same field, different operator" do it "returns string comparison of operator for same field, different operator" do
(SymbolOperator.new(:foo, 'in') <=> SymbolOperator.new(:foo, 'all')).should == 1 (SymbolOperator.new(:foo, 'in') <=> SymbolOperator.new(:foo, 'all')).should == 1
Expand Down

0 comments on commit 208c7c9

Please sign in to comment.