-
Notifications
You must be signed in to change notification settings - Fork 539
Expand file tree
/
Copy pathresource_identity.rb
More file actions
43 lines (37 loc) · 1.09 KB
/
resource_identity.rb
File metadata and controls
43 lines (37 loc) · 1.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
module JSONAPI
# ResourceIdentity describes a unique identity of a resource in the system.
# This consists of a Resource class and an identifier that is unique within
# that Resource class. ResourceIdentities are intended to be used as hash
# keys to provide ordered mixing of resource types in result sets.
#
#
# == Creating a ResourceIdentity
#
# rid = ResourceIdentity.new(PostResource, 12)
#
class ResourceIdentity
attr_reader :resource_klass, :id, :custom_id
def initialize(resource_klass, id, custom_id = nil)
@resource_klass = resource_klass
@id = id
@custom_id = custom_id
end
def ==(other)
# :nocov:
eql?(other)
# :nocov:
end
def eql?(other)
other.is_a?(ResourceIdentity) && other.resource_klass == @resource_klass && other.id == @id && other.custom_id == @custom_id
end
def hash
[@resource_klass, @id, @custom_id].hash
end
# Creates a string representation of the identifier.
def to_s
# :nocov:
[resource_klass, id, custom_id].compact.join(':')
# :nocov:
end
end
end