Sha256: 91417142013703f7e278001744547c2f314ed0055712620b85d53acfc9b69ee9

Contents?: true

Size: 1.4 KB

Versions: 4

Compression:

Stored size: 1.4 KB

Contents

# frozen_string_literal: true

module Checkpoint
  class Resource
    # Specialized Resource type to represent all entities of a particular type.
    class AllOfType < Resource
      attr_reader :type
      # Create a wildcard Resource for a given type
      def initialize(type)
        @type = type
      end

      # Create a type-specific wildcard Resource from a given entity
      #
      # When the entity implements to #to_resource, we convert it first and take
      # the type from the result. Otherwise, when it implements #resource_type,
      # we use that result. Otherwise, we take the class name of the entity.
      # Regardless of the source, the type is forced to a string.
      def self.from(entity)
        new(type_of(entity))
      end

      # This is always the special ALL resource ID
      def id
        Resource::ALL
      end

      # Compares with another Resource
      #
      # @return [Boolean] true if `other` is a Resource and its #type matches.
      def eql?(other)
        other.is_a?(Resource) && type == other.type
      end

      # Private type name extraction
      def self.type_of(entity)
        if entity.respond_to?(:to_resource)
          entity.to_resource.type
        elsif entity.respond_to?(:resource_type)
          entity.resource_type
        else
          entity.class
        end.to_s
      end

      private_class_method :type_of
      alias == eql?
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
checkpoint-1.0.3 lib/checkpoint/resource/all_of_type.rb
checkpoint-1.0.2 lib/checkpoint/resource/all_of_type.rb
checkpoint-1.0.1 lib/checkpoint/resource/all_of_type.rb
checkpoint-1.0.0 lib/checkpoint/resource/all_of_type.rb