Skip to content

Commit 8a522d4

Browse files
committed
Added support to check for equality of two resources
* Updated redundant specs * Updated specs to check for resource equality
1 parent a4472ae commit 8a522d4

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

lib/intercom/traits/api_resource.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ def initialize(attributes = {})
1717
from_hash(attributes)
1818
end
1919

20+
def ==(other)
21+
self.class == other.class && to_json == other.to_json
22+
end
23+
2024
def from_response(response)
2125
from_hash(response)
2226
reset_changed_fields!
@@ -37,6 +41,15 @@ def to_hash
3741
end
3842
end
3943

44+
def to_json(*args)
45+
instance_variables_excluding_dirty_tracking_field.each_with_object({}) do |variable, hash|
46+
next if variable == :@client
47+
48+
value = instance_variable_get(variable)
49+
hash[variable.to_s.delete('@')] = value.respond_to?(:to_json) ? value.to_json(*args) : value
50+
end
51+
end
52+
4053
def to_submittable_hash
4154
submittable_hash = {}
4255
to_hash.each do |attribute, value|

spec/unit/intercom/traits/api_resource_spec.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@
112112
it 'an initialized ApiResource is equal to one generated from a response' do
113113
class ConcreteApiResource; include Intercom::Traits::ApiResource; end
114114
initialized_api_resource = ConcreteApiResource.new(object_json)
115-
except(object_json, 'type', 'nested_fields').keys.each do |attribute|
115+
except(object_json, 'type').keys.each do |attribute|
116116
assert_equal initialized_api_resource.send(attribute), api_resource.send(attribute)
117117
end
118118
end
@@ -122,11 +122,33 @@ class ConcreteApiResource; include Intercom::Traits::ApiResource; end
122122

123123
api_resource.from_hash(object_hash)
124124
initialized_api_resource = ConcreteApiResource.new(object_hash)
125-
except(object_json, 'type', 'nested_fields').keys.each do |attribute|
125+
except(object_json, 'type').keys.each do |attribute|
126126
assert_equal initialized_api_resource.send(attribute), api_resource.send(attribute)
127127
end
128128
end
129129

130+
describe 'correctly equates two resources' do
131+
class DummyResource; include Intercom::Traits::ApiResource; end
132+
133+
specify 'if each resource has the same class and same value' do
134+
api_resource1 = DummyResource.new(object_json)
135+
api_resource2 = DummyResource.new(object_json)
136+
assert_equal (api_resource1 == api_resource2), true
137+
end
138+
139+
specify 'if each resource has the same class and different value' do
140+
object2_json = object_json.merge('id' => 'bbbbbb')
141+
api_resource1 = DummyResource.new(object_json)
142+
api_resource2 = DummyResource.new(object2_json)
143+
assert_equal (api_resource1 == api_resource2), false
144+
end
145+
146+
specify 'if each resource has a different class' do
147+
dummy_resource = DummyResource.new(object_json)
148+
assert_equal (dummy_resource == api_resource), false
149+
end
150+
end
151+
130152
it 'correctly generates submittable hash when no updates' do
131153
assert_equal api_resource.to_submittable_hash, {}
132154
end

0 commit comments

Comments
 (0)