Sha256: 591f96b90ee4d8a1e5be591e3610b2a38e359fcd42aeb471be21840995550d41

Contents?: true

Size: 1.46 KB

Versions: 6

Compression:

Stored size: 1.46 KB

Contents

module GraphitiGql
  class Schema
    class Registry
      include Singleton

      def initialize
        clear
      end

      def get(object, interface: true)
        @data[key_for(object, interface: interface)]
      end

      def set(resource, type, interface: true)
        @data[key_for(resource, interface: interface)] = { resource: resource, type: type, interface: interface }
      end

      def key_for(object, interface: true)
        if object.ancestors.include?(Graphiti::Resource) 
          key = key_for_resource(object)
          if object.polymorphic?
            if !object.polymorphic_child? && interface
              key = "I#{key}"
            end
          end
          key
        else
          raise 'unknown object!'
        end
      end

      def clear
        @data = {}
      end

      def []=(key, value)
        @data[key] = value
      end

      def [](key)
        @data[key]
      end

      def key?(key)
        @data.key?(key)
      end

      def values
        @data.values
      end

      # When polymorphic parent, returns the Interface not the Class
      def resource_types
        values
          .select { |v| v.key?(:resource) }
          .reject { |v| v[:interface] || v[:resource].value_object? }
          .map { |registered| get(registered[:resource]) }
      end

      private

      def key_for_resource(resource)
        resource.graphql_name ||
          resource.name.gsub('Resource', '').gsub('::', '')
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
graphiti_gql-0.2.29 lib/graphiti_gql/schema/registry.rb
graphiti_gql-0.2.28 lib/graphiti_gql/schema/registry.rb
graphiti_gql-0.2.27 lib/graphiti_gql/schema/registry.rb
graphiti_gql-0.2.26 lib/graphiti_gql/schema/registry.rb
graphiti_gql-0.2.25 lib/graphiti_gql/schema/registry.rb
graphiti_gql-0.2.24 lib/graphiti_gql/schema/registry.rb