Skip to content

Commit eaa6821

Browse files
authored
Merge pull request #553 from zendesk/RED-1856-eq-cleanup
Red 1856 eq cleanup
2 parents bdb733b + 55b69e5 commit eaa6821

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

lib/zendesk_api/resource.rb

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,14 @@ def resource_path
3939
def namespace(namespace)
4040
@namespace = namespace
4141
end
42+
43+
def new_from_response(client, response, includes = nil)
44+
new(client).tap do |resource|
45+
resource.handle_response(response)
46+
resource.set_includes(resource, includes, response.body) if includes
47+
resource.attributes.clear_changes
48+
end
49+
end
4250
end
4351

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

124132
# Compares resources by class and id. If id is nil, then by object_id
125133
def ==(other)
134+
return false unless other
135+
126136
return true if other.object_id == object_id
127137

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

132-
if other.is_a?(Data)
133-
other.id && other.id == id
134-
elsif other.is_a?(Integer)
135-
id == other
136-
else
137-
false
138-
end
140+
return id == other if other.is_a?(Integer)
141+
142+
warn "Trying to compare #{other.class} to a Resource
143+
from #{caller.first}"
139144
end
140145
alias :eql :==
141146

spec/core/data_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'core/spec_helper'
2+
3+
RSpec.describe ZendeskAPI::Data do
4+
describe ".new_from_response" do
5+
let(:response) { double(:response) }
6+
7+
it "returns an instance with the response" do
8+
expect(described_class.new_from_response(client, response))
9+
.to be_instance_of(described_class)
10+
end
11+
end
12+
end

0 commit comments

Comments
 (0)