Skip to content

Implement eql and hash for Builder class #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- [#338](https://github.com/JsonApiClient/json_api_client/pull/338) - implement hash and eql? for builder class
- [#351](https://github.com/JsonApiClient/json_api_client/pull/351) - Remove rudimental `last_result_set` relationship from serializer

## 1.13.0
Expand Down
14 changes: 14 additions & 0 deletions lib/json_api_client/query/builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,20 @@ def method_missing(method_name, *args, &block)
to_a.send(method_name, *args, &block)
end

def hash
[
klass,
params
].hash
end

def ==(other)
return false unless other.is_a?(self.class)

hash == other.hash
end
alias_method :eql?, :==

protected

def _fetch
Expand Down
20 changes: 20 additions & 0 deletions test/unit/query_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -312,4 +312,24 @@ def test_build_propagate_only_path_params
assert_equal '123', record.author_id
assert_equal [], record.relationships.changed
end

def test_build_hash_sum
a = ArticleNested.where(author_id: '123', name: 'John')
b = ArticleNested.where(author_id: '123', name: 'John')
c = ArticleNested.where(author_id: '123')
d = Article.where(author_id: '123', name: 'John')
assert(a.hash == b.hash)
assert_equal(false, a.hash == c.hash)
assert_equal(false, a.hash == d.hash)
end

def test_build_eql
a = ArticleNested.where(author_id: '123', name: 'John')
b = ArticleNested.where(author_id: '123', name: 'John')
c = ArticleNested.where(author_id: '123')
d = Article.where(author_id: '123', name: 'John')
assert(a == b)
assert_equal(false, a == c)
assert_equal(false, a == d)
end
end