Sha256: 3b2c0f1a50a0672bd4505cd4d2f3eaee97f77dca61b737c8c1cff983e8deaf79

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

# frozen_string_literal: true

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

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

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

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

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

    def <=>(other_identity)
      self.id <=> other_identity.id
    end

    # Creates a string representation of the identifier.
    def to_s
      # :nocov:
      "#{resource_klass}:#{id}"
      # :nocov:
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jsonapi-resources-0.11.0.beta2 lib/jsonapi/resource_identity.rb