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.



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

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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



14
15
16
# File 'lib/jsonapi/resource_identity.rb', line 14

def id
  @id
end

#resource_klassObject (readonly)

Returns the value of attribute resource_klass.



14
15
16
# File 'lib/jsonapi/resource_identity.rb', line 14

def resource_klass
  @resource_klass
end

Instance Method Details

#==(other) ⇒ Object



21
22
23
24
25
# File 'lib/jsonapi/resource_identity.rb', line 21

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

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/jsonapi/resource_identity.rb', line 27

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

#hashObject



31
32
33
# File 'lib/jsonapi/resource_identity.rb', line 31

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

#to_sObject

Creates a string representation of the identifier.



36
37
38
39
40
# File 'lib/jsonapi/resource_identity.rb', line 36

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