<commit_before>module Adrift
  class UpFile
    module Proxies
      module Rack
        def original_filename
          @up_file_representation[:filename]
        end

        def tempfile
          @up_file_representation[:tempfile]
        end
      end

      module Rails
        def original_filename
          @up_file_representation.original_filename
        end

        def tempfile
          @up_file_representation.tempfile
        end
      end

      module File
        def original_filename
          ::File.basename(@up_file_representation)
        end

        def tempfile
          @up_file_representation
        end
      end
    end

    def initialize(up_file_representation)
      if up_file_representation.respond_to?(:has_key?) && up_file_representation.has_key?(:filename) && up_file_representation.has_key?(:tempfile)
        extend Proxies::Rack
      elsif up_file_representation.respond_to?(:original_filename) && up_file_representation.respond_to?(:tempfile)
        extend Proxies::Rails
      elsif up_file_representation.respond_to?(:path)
        extend Proxies::File
      else
        raise UnknownUpFileError
      end

      @up_file_representation = up_file_representation
    end

    def path
      tempfile.path
    end
  end
end
<commit_msg>refactor Adrift::UpFile<commit_after>module Adrift
  class UpFile
    module Proxy
      def initialize(up_file_representation)
        @up_file_representation = up_file_representation
      end

      def path
        tempfile.path
      end
    end

    module Proxies
      class Rack
        include Proxy

        def self.recognize?(up_file_representation)
          up_file_representation.respond_to?(:has_key?) &&
            up_file_representation.has_key?(:filename) &&
            up_file_representation.has_key?(:tempfile)
        end

        def original_filename
          @up_file_representation[:filename]
        end

        def tempfile
          @up_file_representation[:tempfile]
        end
      end

      class Rails
        include Proxy

        def self.recognize?(up_file_representation)
          up_file_representation.respond_to?(:original_filename) &&
            up_file_representation.respond_to?(:tempfile)
        end

        def original_filename
          @up_file_representation.original_filename
        end

        def tempfile
          @up_file_representation.tempfile
        end
      end

      class File
        include Proxy

        def self.recognize?(up_file_representation)
          up_file_representation.respond_to?(:to_path)
        end

        def original_filename
          ::File.basename(@up_file_representation)
        end

        def tempfile
          @up_file_representation
        end
      end
    end

    def self.new(up_file_representation)
      proxy = proxy_for(up_file_representation)
      raise UnknownUpFileError if proxy.nil?
      proxy.new(up_file_representation)
    end

    private

    def self.proxy_for(up_file_representation)
      proxies.find do |proxy|
        proxy.respond_to?(:recognize?) &&
          proxy.recognize?(up_file_representation)
      end
    end

    def self.proxies
      Proxies.constants.map { |proxy_name| Proxies.const_get(proxy_name) }
    end
  end
end

{'fraction_non_alphanumeric': 0.05727411944869831, 'fraction_numerical': 0.0, 'mean_word_length': 2.233663366336634, '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': '196764', 'n_tokens_mistral': 966, 'n_tokens_neox': 903, 'n_words': 172}