<commit_before>module GraphQL
  module Relay
    # Subclasses must implement:
    #   - {#cursor_from_node}, which returns an opaque cursor for the given item
    #   - {#sliced_nodes}, which slices by `before` & `after`
    #   - {#paged_nodes}, which applies `first` & `last` limits
    #
    # In a subclass, you have access to
    #   - {#object}, the object which the connection will wrap
    #   - {#first}, {#after}, {#last}, {#before} (arguments passed to the field)
    #
    class BaseConnection
      # Just to encode data in the cursor, use something that won't conflict
      CURSOR_SEPARATOR = "---"

      # Map of collection classes -> connection_classes
      # eg Array -> ArrayConnection
      CONNECTION_IMPLEMENTATIONS = {}

      # Create a connection which exposes edges of this type
      def self.create_type(wrapped_type, &block)
        edge_type = wrapped_type.edge_type

        connection_type = ObjectType.define do
          name("#{wrapped_type.name}Connection")
          field :edges, types[edge_type]
          field :pageInfo, PageInfo, property: :page_info
          block && instance_eval(&block)
        end

        connection_type
      end

      # Find a connection implementation suitable for exposing `items`
      #
      # @param [Object] A collection of items (eg, Array, AR::Relation)
      # @return [subclass of BaseConnection] a connection Class for wrapping `items`
      def self.connection_for_items(items)
        implementation = CONNECTION_IMPLEMENTATIONS.find do |items_class_name, connection_class|
          items.class.ancestors.map(&:name).include? items_class_name
        end
        if implementation.nil?
          raise("No connection implementation to wrap #{items.class} (#{items})")
        else
          implementation[1]
        end
      end

      # Add `connection_class` as the connection wrapper for `items_class`
      # eg, `RelationConnection` is the implementation for `AR::Relation`
      # @param [Class] A class representing a collection (eg, Array, AR::Relation)
      # @param [Class] A class implementing Connection methods
      def self.register_connection_implementation(items_class, connection_class)
        CONNECTION_IMPLEMENTATIONS[items_class.name] = connection_class
      end

      attr_reader :object, :arguments

      def initialize(object, arguments)
        @object = object
        @arguments = arguments
      end
<commit_msg>add comment, small optimization<commit_after>module GraphQL
  module Relay
    # Subclasses must implement:
    #   - {#cursor_from_node}, which returns an opaque cursor for the given item
    #   - {#sliced_nodes}, which slices by `before` & `after`
    #   - {#paged_nodes}, which applies `first` & `last` limits
    #
    # In a subclass, you have access to
    #   - {#object}, the object which the connection will wrap
    #   - {#first}, {#after}, {#last}, {#before} (arguments passed to the field)
    #
    class BaseConnection
      # Just to encode data in the cursor, use something that won't conflict
      CURSOR_SEPARATOR = "---"

      # Map of collection classes -> connection_classes
      # eg Array -> ArrayConnection
      CONNECTION_IMPLEMENTATIONS = {}

      # Create a connection which exposes edges of this type
      def self.create_type(wrapped_type, &block)
        edge_type = wrapped_type.edge_type

        connection_type = ObjectType.define do
          name("#{wrapped_type.name}Connection")
          field :edges, types[edge_type]
          field :pageInfo, PageInfo, property: :page_info
          block && instance_eval(&block)
        end

        connection_type
      end

      # Find a connection implementation suitable for exposing `items`
      #
      # @param [Object] A collection of items (eg, Array, AR::Relation)
      # @return [subclass of BaseConnection] a connection Class for wrapping `items`
      def self.connection_for_items(items)
        # We check class membership by comparing class names rather than
        # identity to prevent this from being broken by Rails autoloading.
        # Changes to the source file for ItemsClass in Rails apps cause it to be
        # reloaded as a new object, so if we were to use `is_a?` here, it would
        # no longer match any registered custom connection types.
        ancestor_names = items.class.ancestors.map(&:name)
        implementation = CONNECTION_IMPLEMENTATIONS.find do |items_class_name, connection_class|
          ancestor_names.include? items_class_name
        end
        if implementation.nil?
          raise("No connection implementation to wrap #{items.class} (#{items})")
        else
          implementation[1]
        end
      end

      # Add `connection_class` as the connection wrapper for `items_class`
      # eg, `RelationConnection` is the implementation for `AR::Relation`
      # @param [Class] A class representing a collection (eg, Array, AR::Relation)
      # @param [Class] A class implementing Connection methods
      def self.register_connection_implementation(items_class, connection_class)
        CONNECTION_IMPLEMENTATIONS[items_class.name] = connection_class
      end

      attr_reader :object, :arguments

      def initialize(object, arguments)
        @object = object
        @arguments = arguments
      end

{'fraction_non_alphanumeric': 0.08221761267207241, 'fraction_numerical': 0.0003771450122572129, 'mean_word_length': 2.8130841121495327, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '957320', 'n_tokens_mistral': 1440, 'n_tokens_neox': 1382, 'n_words': 476}