<commit_before>module RedisOrm
  module Associations
    class HasManyProxy
      def initialize(reciever_model_name, reciever_id, foreign_models)
        @records = [] #records.to_a
        @reciever_model_name = reciever_model_name
        @reciever_id = reciever_id
        @foreign_models = foreign_models
        @fetched = false
      end

      def fetch
        @records = @foreign_models.to_s.singularize.camelize.constantize.find($redis.zrevrangebyscore "#{@reciever_model_name}:#{@reciever_id}:#{@foreign_models}", Time.now.to_i, 0)
        @fetched = true
      end

      def [](index)
        fetch if !@fetched
        @records[index]
      end

      # user = User.find(1)
      # user.avatars << Avatar.find(23) => user:1:avatars => [23]
      def <<(new_records)
        new_records.to_a.each do |record|
          $redis.zadd("#{@reciever_model_name}:#{@reciever_id}:#{record.model_name.pluralize}", Time.now.to_i, record.id)

          # article.comments << [comment1, comment2] 
          # iterate through the array of comments and create backlink
          # check whether *record* object has *has_many* declaration and TODO it states *self.model_name* in plural and there is no record yet from the *record*'s side (in order not to provoke recursion)
          
          if record.get_associations.detect{|h| h[:type] == :has_many && h[:foreign_models] == @reciever_model_name.pluralize.to_sym} && !$redis.zrank("#{record.model_name}:#{record.id}:#{@reciever_model_name.pluralize}", @reciever_id) #record.model_name.to_s.camelize.constantize.find(@reciever_id).nil?
            $redis.zadd("#{record.model_name}:#{record.id}:#{@reciever_model_name.pluralize}", Time.now.to_i, @reciever_id)
          # check whether *record* object has *has_one* declaration and TODO it states *self.model_name* and there is no record yet from the *record*'s side (in order not to provoke recursion)
          elsif record.get_associations.detect{|h| [:has_one, :belongs_to].include?(h[:type]) && h[:foreign_model] == @reciever_model_name.to_sym} && record.send(@reciever_model_name.to_sym).nil?
            
            $redis.set("#{record.model_name}:#{record.id}:#{@reciever_model_name}", @reciever_id)
            #record.send("#{@reciever_model_name}=", self)            
          end
        end
      end

      def all(options = {})
        if options[:limit] && options[:offset]
          # ZREVRANGEBYSCORE album:ids 1305451611 1305443260 LIMIT 0, 2
          record_ids = $redis.zrevrangebyscore("#{@reciever_model_name}:#{@reciever_id}:#{@foreign_models}", Time.now.to_i, 0, :limit => [options[:offset].to_i, options[:limit].to_i])
          @fetched = true
          @records = @foreign_models.to_s.singularize.camelize.constantize.find(record_ids)
        elsif options[:limit]
          record_ids = $redis.zrevrangebyscore("#{@reciever_model_name}:#{@reciever_id}:#{@foreign_models}", Time.now.to_i, 0, :limit => [0, options[:limit].to_i])
          @fetched = true
          @records = @foreign_models.to_s.singularize.camelize.constantize.find(record_ids)
        else
          fetch if !@fetched
          @records<commit_msg>taking into account [:options][:as] when saving assocs<commit_after>module RedisOrm
  module Associations
    class HasManyProxy
      def initialize(reciever_model_name, reciever_id, foreign_models)
        @records = [] #records.to_a
        @reciever_model_name = reciever_model_name
        @reciever_id = reciever_id
        @foreign_models = foreign_models
        @fetched = false
      end

      def fetch
        @records = @foreign_models.to_s.singularize.camelize.constantize.find($redis.zrevrangebyscore "#{@reciever_model_name}:#{@reciever_id}:#{@foreign_models}", Time.now.to_i, 0)
        @fetched = true
      end

      def [](index)
        fetch if !@fetched
        @records[index]
      end

      # user = User.find(1)
      # user.avatars << Avatar.find(23) => user:1:avatars => [23]
      def <<(new_records)
        new_records.to_a.each do |record|
          $redis.zadd("#{@reciever_model_name}:#{@reciever_id}:#{record.model_name.pluralize}", Time.now.to_i, record.id)
          record_associations = record.get_associations
          # article.comments << [comment1, comment2] 
          # iterate through the array of comments and create backlink
          # check whether *record* object has *has_many* declaration and TODO it states *self.model_name* in plural and there is no record yet from the *record*'s side (in order not to provoke recursion)                    
          if has_many_assoc = record_associations.detect{|h| h[:type] == :has_many && h[:foreign_models] == @reciever_model_name.pluralize.to_sym}
            pluralized_reciever_model_name = if has_many_assoc[:options][:as]
              has_many_assoc[:options][:as].pluralize
            else
              @reciever_model_name.pluralize
            end
            if !$redis.zrank("#{record.model_name}:#{record.id}:#{pluralized_reciever_model_name}", @reciever_id)
              $redis.zadd("#{record.model_name}:#{record.id}:#{pluralized_reciever_model_name}", Time.now.to_i, @reciever_id)
            end
          # check whether *record* object has *has_one* declaration and TODO it states *self.model_name* and there is no record yet from the *record*'s side (in order not to provoke recursion)
          elsif has_one_assoc = record_associations.detect{|h| [:has_one, :belongs_to].include?(h[:type]) && h[:foreign_model] == @reciever_model_name.to_sym}
            reciever_model_name = if has_one_assoc[:options][:as]
              has_one_assoc[:options][:as].to_sym
            else
              @reciever_model_name
            end
            if record.send(reciever_model_name).nil?
              $redis.set("#{record.model_name}:#{record.id}:#{reciever_model_name}", @reciever_id)
            end
          end
        end
      end

      def all(options = {})
        if options[:limit] && options[:offset]
          # ZREVRANGEBYSCORE album:ids 1305451611 1305443260 LIMIT 0, 2
          record_ids = $redis.zrevrangebyscore("#{@reciever_model_name}:#{@reciever_id}:#{@foreign_models}", Time.now.to_i, 0, :limit => [options[:offset].to_i, options[:limit].to_i])
          @fetched = true
          @records = @foreign_models.to_s.singularize.camelize.constantize.find(record_ids)
        elsif options[:limit]
          record_ids = $redis.zrevrangebyscore("#{@reciever_model_name}:#{@reciever_id}:#{@foreign_models}", Time.now.to_i, 0, :limit => [0, options[:limit].to_i])
          @fetched = true
          @records = @foreign_models.to_s.singularize.camelize.constantize.find(record_ids)
        else
          fetch if !@fetched
          @records
{'fraction_non_alphanumeric': 0.12819370172311348, 'fraction_numerical': 0.010101010101010102, 'mean_word_length': 3.0954987834549876, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, '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': '6805697', 'n_tokens_mistral': 2307, 'n_tokens_neox': 2232, 'n_words': 402}