<commit_before>class Serializr
  # Extension for AbstactController::Base subclasses like ActionController::Base
  # or ActionController::Metal.
  #
  # Introduces a `:serializer` option that is automatically inferred by the
  # resource to be rendered in `render :json`. Setting it explicitly prevents
  # the inferring altogether.
  module AbstractController
    def _normalize_options(options)
      # The resource can be nil, check if it is explicitly passed as such.
      if options.key?(:json)
        resource = options[:json]
        serializer = options[:serializer] || begin
          if resource.respond_to?(:to_ary)
            # Assume a collection if the resource responds to #to_ary. The
            # regular arrays and ActiveRecord::Relations do and will be
            # represented as arrays, while hashes won't be
            inferred = serializer_class_cache[resource.to_ary.first.class]
            inferred[] if inferred
          else
            serializer_class_cache[resource.class]
          end
        end

        options[:json] = serializer.new(resource, options) if serializer
      end

      super
    end

    private

    def serializer_class_cache
      @@_serializer_class_cache ||= Hash.new do |_, cls|
        "#{cls}Serializer".safe_constantize ||
          # Try to infer the superclass, if someone let a serializer for it and
          # not the concrete classes. Don't go deeper, though, just one level
          # is enough.
          "#{cls.superclass}Serializer".safe_constantize<commit_msg>Grammar and sentences and stuff<commit_after>class Serializr
  # Extension for AbstactController::Base subclasses like ActionController::Base
  # or ActionController::Metal.
  #
  # Introduces a `:serializer` option that is automatically inferred by the
  # resource to be rendered in `render :json`. Setting it explicitly prevents
  # the inferring altogether.
  module AbstractController
    def _normalize_options(options)
      # The resource can be nil, check if it is explicitly passed as such.
      if options.key?(:json)
        resource = options[:json]
        serializer = options[:serializer] || begin
          if resource.respond_to?(:to_ary)
            # Assume a collection if the resource responds to #to_ary. The
            # regular arrays and ActiveRecord::Relations do and will be
            # represented as arrays, while hashes won't be.
            inferred = serializer_class_cache[resource.to_ary.first.class]
            inferred[] if inferred
          else
            serializer_class_cache[resource.class]
          end
        end

        options[:json] = serializer.new(resource, options) if serializer
      end

      super
    end

    private

    def serializer_class_cache
      @@_serializer_class_cache ||= Hash.new do |_, cls|
        "#{cls}Serializer".safe_constantize ||
          # Try to infer the superclass, if someone let a serializer for it and
          # not the concrete classes. Don't go deeper, though, just one level
          # is enough.
          "#{cls.superclass}Serializer".safe_constantize
{'fraction_non_alphanumeric': 0.07135938004520503, 'fraction_numerical': 0.0, 'mean_word_length': 2.6533018867924527, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '1541428', 'n_tokens_mistral': 812, 'n_tokens_neox': 793, 'n_words': 277}