Class: JSONAPI::ResourceIdentity

Inherits:
Object
  • Object
show all
Defined in:
lib/jsonapi/resource_identity.rb

Overview

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)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource_klass, id) ⇒ ResourceIdentity

Returns a new instance of ResourceIdentity.



18
19
20
21
# File 'lib/jsonapi/resource_identity.rb', line 18

def initialize(resource_klass, id)
  @resource_klass = resource_klass
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



16
17
18
# File 'lib/jsonapi/resource_identity.rb', line 16

def id
  @id
end

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



16
17
18
# File 'lib/jsonapi/resource_identity.rb', line 16

def resource_klass
  @resource_klass
end

Instance Method Details

#==(other) ⇒ Object



23
24
25
26
27
# File 'lib/jsonapi/resource_identity.rb', line 23

def ==(other)
  # :nocov:
  eql?(other)
  # :nocov:
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


29
30
31
# File 'lib/jsonapi/resource_identity.rb', line 29

def eql?(other)
  other.is_a?(ResourceIdentity) && other.resource_klass == @resource_klass && other.id == @id
end

#hashObject



33
34
35
# File 'lib/jsonapi/resource_identity.rb', line 33

def hash
  [@resource_klass, @id].hash
end

#to_sObject

Creates a string representation of the identifier.



38
39
40
41
42
# File 'lib/jsonapi/resource_identity.rb', line 38

def to_s
  # :nocov:
  "#{resource_klass}:#{id}"
  # :nocov:
end