From 987eae9d9b45d0e0839b8ebab63f626da35bd87a Mon Sep 17 00:00:00 2001 From: Rogers Date: Wed, 17 May 2023 11:00:32 -0500 Subject: [PATCH 1/3] Small refactor and a bit clean up --- lib/zendesk_api/resource.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/zendesk_api/resource.rb b/lib/zendesk_api/resource.rb index 6a1bbe57..4f695699 100644 --- a/lib/zendesk_api/resource.rb +++ b/lib/zendesk_api/resource.rb @@ -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 @@ -123,17 +131,17 @@ def to_s # Compares resources by class and id. If id is nil, then by object_id def ==(other) - return true if other.object_id == object_id + return false unless other - if other && !(other.is_a?(Data) || other.is_a?(Integer)) - warn "Trying to compare #{other.class} to a Resource from #{caller.first}" - end + return true if other.object_id == object_id if other.is_a?(Data) other.id && other.id == id elsif other.is_a?(Integer) id == other else + warn "Trying to compare #{other.class} to a Resource from #{caller.first}" + false end end From 2f05515c9275437c0582a8b5d45b41ac4cbeed08 Mon Sep 17 00:00:00 2001 From: Rogers Date: Thu, 18 May 2023 10:32:14 -0500 Subject: [PATCH 2/3] Clean up a bit more --- lib/zendesk_api/resource.rb | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/lib/zendesk_api/resource.rb b/lib/zendesk_api/resource.rb index 4f695699..889922f7 100644 --- a/lib/zendesk_api/resource.rb +++ b/lib/zendesk_api/resource.rb @@ -135,15 +135,14 @@ def ==(other) return true if other.object_id == object_id - if other.is_a?(Data) - other.id && other.id == id - elsif other.is_a?(Integer) - id == other - else - warn "Trying to compare #{other.class} to a Resource from #{caller.first}" - - false + return other.id && (other.id == id) if other.is_a?(Data) + + unless other.is_a?(Integer) + return warn "Trying to compare #{other.class} to a Resource + from #{caller.first}" end + + id == other end alias :eql :== From e4dce9719cfaf4645114ebec8de7c424a2c5c9ec Mon Sep 17 00:00:00 2001 From: Rogers Date: Thu, 8 Jun 2023 20:52:37 -0500 Subject: [PATCH 3/3] Replace unless with a guard clause --- lib/zendesk_api/resource.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/zendesk_api/resource.rb b/lib/zendesk_api/resource.rb index 889922f7..b0d35282 100644 --- a/lib/zendesk_api/resource.rb +++ b/lib/zendesk_api/resource.rb @@ -137,12 +137,10 @@ def ==(other) return other.id && (other.id == id) if other.is_a?(Data) - unless other.is_a?(Integer) - return warn "Trying to compare #{other.class} to a Resource - from #{caller.first}" - end + return id == other if other.is_a?(Integer) - id == other + warn "Trying to compare #{other.class} to a Resource + from #{caller.first}" end alias :eql :==