Skip to content

Commit

Permalink
Merge pull request #553 from zendesk/RED-1856-eq-cleanup
Browse files Browse the repository at this point in the history
Red 1856 eq cleanup
  • Loading branch information
ecoologic authored Aug 22, 2023
2 parents bdb733b + 55b69e5 commit eaa6821
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
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
12 changes: 12 additions & 0 deletions spec/core/data_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require 'core/spec_helper'

RSpec.describe ZendeskAPI::Data do
describe ".new_from_response" do
let(:response) { double(:response) }

it "returns an instance with the response" do
expect(described_class.new_from_response(client, response))
.to be_instance_of(described_class)
end
end
end

0 comments on commit eaa6821

Please sign in to comment.