<commit_before>module ActiveModelSerializers
  module Adapter
    class Attributes < Base
      def initialize(serializer, options = {})
        super
        @include_directive =
          if options[:include_directive]
            options[:include_directive]
          elsif options[:include]
            JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
          else
            ActiveModelSerializers.default_include_directive
          end
      end

      def serializable_hash(options = nil)
        options = serialization_options(options)

        if serializer.respond_to?(:each)
          serializable_hash_for_collection(options)
        else
          serializable_hash_for_single_resource(options)
        end
      end

      private

      def serializable_hash_for_collection(options)
        instance_options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(serializer, self, @include_directive)
        opts = instance_options.merge(include_directive: @include_directive)
        serializer.map { |s| Attributes.new(s, opts).serializable_hash(options) }
      end

      def serializable_hash_for_single_resource(options)
        cached_attributes = instance_options[:cached_attributes] || {}
        resource = serializer.cached_attributes(options[:fields], cached_attributes, self)
        relationships = resource_relationships(options)
        resource.merge(relationships)
      end

      def resource_relationships(options)
        relationships = {}
        serializer.associations(@include_directive).each do |association|
          relationships[association.key] ||= relationship_value_for(association, options)
        end

        relationships
      end

      def relationship_value_for(association, options)
        return association.options[:virtual_value] if association.options[:virtual_value]
        return unless association.serializer && association.serializer.object

        opts = instance_options.merge(include_directive: @include_directive[association.key])
        relationship_value = Attributes.new(association.serializer, opts).serializable_hash(options)

        if association.options[:polymorphic] && relationship_value
          polymorphic_type = association.serializer.object.class.name.underscore
          relationship_value = { type: polymorphic_type, polymorphic_type.to_sym => relationship_value }
        end

        relationship_value
      end
    end
  end
end<commit_msg>Remove Attributes adapter recursion<commit_after>module ActiveModelSerializers
  module Adapter
    class Attributes < Base
      def initialize(serializer, options = {})
        super
      end

      def serializable_hash(options = nil)
        options = serialization_options(options)

        if serializer.respond_to?(:each)
          serializable_hash_for_collection(serializer, options)
        else
          serializable_hash_for_single_resource(serializer, instance_options, options)
        end
      end

      private

      def include_directive_from_options(options)
        if options[:include_directive]
          options[:include_directive]
        elsif options[:include]
          JSONAPI::IncludeDirective.new(options[:include], allow_wildcard: true)
        else
          ActiveModelSerializers.default_include_directive
        end
      end

      def serializable_hash_for_collection(serializers, options)
        include_directive = include_directive_from_options(instance_options)
        instance_options[:cached_attributes] ||= ActiveModel::Serializer.cache_read_multi(serializers, self, include_directive)
        instance_opts = instance_options.merge(include_directive: include_directive)
        serializers.map do |serializer|
          serializable_hash_for_single_resource(serializer, instance_opts, options)
        end
      end

      def serializable_hash_for_single_resource(serializer, instance_options, options)
        options[:include_directive] ||= include_directive_from_options(instance_options)
        cached_attributes = instance_options[:cached_attributes] ||= {}
        resource = serializer.cached_attributes(options[:fields], cached_attributes, self)
        relationships = resource_relationships(serializer, options)
        resource.merge(relationships)
      end

      def resource_relationships(serializer, options)
        relationships = {}
        include_directive = options.fetch(:include_directive)
        serializer.associations(include_directive).each do |association|
          relationships[association.key] ||= relationship_value_for(association, options)
        end

        relationships
      end

      def relationship_value_for(association, options)
        return association.options[:virtual_value] if association.options[:virtual_value]
        return unless association.serializer && association.serializer.object

        include_directive = options.fetch(:include_directive)
        opts = instance_options.merge(include_directive: include_directive[association.key])
        relationship_value = Attributes.new(association.serializer, opts).serializable_hash(options)

        if association.options[:polymorphic] && relationship_value
          polymorphic_type = association.serializer.object.class.name.underscore
          relationship_value = { type: polymorphic_type, polymorphic_type.to_sym => relationship_value }
        end

        relationship_value
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.07019794721407624, 'fraction_numerical': 0.0, 'mean_word_length': 3.4257907542579074, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '4933495', 'n_tokens_mistral': 1405, 'n_tokens_neox': 1364, 'n_words': 239}