<commit_before>module Refile
  module Attachment
    # @api private
    class Attachment
      attr_reader :record, :name, :cache, :store, :cache_id, :options, :errors
      attr_accessor :remove

      def initialize(record, name, **options)
        @record = record
        @name = name
        @options = options
        @cache = Refile.backends.fetch(@options[:cache].to_s)
        @store = Refile.backends.fetch(@options[:store].to_s)
        @errors = []
      end

      def id
        record.send(:"#{name}_id")
      end

      def id=(id)
        record.send(:"#{name}_id=", id)
      end

      def file
        if cache_id and not cache_id == ""
          cache.get(cache_id)
        elsif id and not id == ""
          store.get(id)
        end
      end

      def file=(uploadable)
        @cache_file = cache.upload(uploadable)
        @cache_id = @cache_file.id
        @errors = []
      rescue Refile::Invalid
        @errors = [:too_large]
        raise if @options[:raise_errors]
      end

      def download(url)
        if url and not url == ""
          raw_response = RestClient::Request.new(method: :get, url: url, raw_response: true).execute
          self.file = raw_response.file
        end
      rescue RestClient::Exception
        @errors = [:download_failed]
        raise if @options[:raise_errors]
      end
<commit_msg>Refactor<commit_after>module Refile
  module Attachment
    # @api private
    class Attachment
      attr_reader :record, :name, :cache, :store, :cache_id, :options, :errors
      attr_accessor :remove

      def initialize(record, name, **options)
        @record = record
        @name = name
        @options = options
        @cache = Refile.backends.fetch(@options[:cache].to_s)
        @store = Refile.backends.fetch(@options[:store].to_s)
        @errors = []
      end

      def id
        record.send(:"#{name}_id")
      end

      def id=(id)
        record.send(:"#{name}_id=", id)
      end

      def file
        if cached?
          cache.get(cache_id)
        elsif id and not id == ""
          store.get(id)
        end
      end

      def file=(uploadable)
        @cache_file = cache.upload(uploadable)
        @cache_id = @cache_file.id
        @errors = []
      rescue Refile::Invalid
        @errors = [:too_large]
        raise if @options[:raise_errors]
      end

      def download(url)
        if url and not url == ""
          raw_response = RestClient::Request.new(method: :get, url: url, raw_response: true).execute
          self.file = raw_response.file
        end
      rescue RestClient::Exception
        @errors = [:download_failed]
        raise if @options[:raise_errors]
      end

{'fraction_non_alphanumeric': 0.11359404096834265, 'fraction_numerical': 0.0, 'mean_word_length': 2.004474272930649, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1517836', 'n_tokens_mistral': 812, 'n_tokens_neox': 807, 'n_words': 195}