Sha256: ec78925f7c10bd34c0377f9344af61d49bec5a5f6ae2dfcf364909996e9964b3

Contents?: true

Size: 1.28 KB

Versions: 2

Compression:

Stored size: 1.28 KB

Contents

module JSONAPIonify::Structure
  module Objects
    class ResourceIdentifier < Base
      # A resource object **MUST** contain at least the following top-level members:
      must_contain! :id, :type # Describes ResourceObjects that share common attributes and relationships.

      # Identification
      # ==============
      #
      # The values of the `id` and `type` members **MUST** be strings.
      type_of! :id, must_be: String
      type_of! :type, must_be: String

      # The values of `type` members **MUST** adhere to the same constraints as member names.
      validate!(:type, message: 'is not a valid member name') do |*, value|
        Helpers::MemberNames.valid? value
      end

      validate_object!(with: :duplicate_does_not_exist?, message: 'is not unique')

      def duplicate_exists?
        return false unless parent.is_a?(Array)
        peers = parent - [self]
        peers.any? { |peer| same_as? peer }
      end

      def duplicate_does_not_exist?
        !duplicate_exists?
      end

      def same_as?(other)
        return false unless other.is_a? ResourceIdentifier
        matches_id   = other.has_key?(:id) && has_key?(:id) && other[:id] == self[:id]
        matches_type = other[:type] == self[:type]
        matches_type && matches_id
      end

    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jsonapionify-0.9.0 lib/jsonapionify/structure/objects/resource_identifier.rb
jsonapionify-0.0.1.pre lib/jsonapionify/structure/objects/resource_identifier.rb