Skip to content

Small refactor and a bit clean up #535

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 3 commits into from
Aug 21, 2023
Merged
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
25 changes: 15 additions & 10 deletions lib/zendesk_api/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def resource_path
def namespace(namespace)
@namespace = namespace
end

def new_from_response(client, response, includes = nil)
new(client).tap do |resource|
resource.handle_response(response)
resource.set_includes(resource, includes, response.body) if includes
resource.attributes.clear_changes
end
end
end

# @return [Hash] The resource's attributes
Expand Down Expand Up @@ -123,19 +131,16 @@ def to_s

# Compares resources by class and id. If id is nil, then by object_id
def ==(other)
return false unless other

return true if other.object_id == object_id

if other && !(other.is_a?(Data) || other.is_a?(Integer))
warn "Trying to compare #{other.class} to a Resource from #{caller.first}"
end
return other.id && (other.id == id) if other.is_a?(Data)

if other.is_a?(Data)
other.id && other.id == id
elsif other.is_a?(Integer)
id == other
else
false
end
return id == other if other.is_a?(Integer)

warn "Trying to compare #{other.class} to a Resource
from #{caller.first}"
end
alias :eql :==

Expand Down