<commit_before>module Mongoid

  # This module contains the core macro for defining listable has_many relationships
  # documents. They can be either embedded or referenced (relational).
  module Lists

    extend ActiveSupport::Concern

    module ClassMethods

      # Macro to set relation on which to make a list
      #
      # @param [ Symbol ] relation The name of the has_many relation
      # @param [ Hash ] options The options hash
      #
      # @return [ Mongoid:Relations:Metadata ] Instance of metadata for the relation
      #
      # @since 0.0.1
      def lists relation, options={}
        meta       = reflect_on_association relation
        field_name = options[:column] || 
          (meta.foreign_key.to_s.gsub(/_?id$/, '_position')).to_sym
        

        # Override model#{name}_ids=        
        before_method self, "#{relation.to_s.singularize}_ids=" do |ids|
          ids.each_with_index do |id, index|
            meta.klass.find(id).update_attribute field_name, index + 1
          end

          meta.klass.where(meta.foreign_key => id).not_in(id: ids).each do |obj|
            obj.update_attribute field_name, nil
          end
        end

        meta[:order] = "#{field_name} asc"
      end

      private

      def before_method owner, method, &block
        original_method = instance_method method
        owner.re_define_method method do |*args|
          block.call *args
          original_method.bind(self).call *args
        end
      end

    end

  end

end<commit_msg>documentation<commit_after>module Mongoid

  # This module contains the core macro for defining listable has_many relationships
  # documents. They can be either embedded or referenced (relational).
  module Lists

    extend ActiveSupport::Concern

    module ClassMethods

      # Macro to set relation on which to make a list
      #
      # @param [ Symbol ] relation The name of the has_many relation
      # @param [ Hash ]   options The options hash
      #
      # @return [ Mongoid:Relations:Metadata ] Instance of metadata for the relation
      #
      # @since 0.0.1
      def lists name, options={}
        meta       = reflect_on_association name
        field_name = options[:column] || 
          (meta.foreign_key.to_s.gsub(/_?id$/, '_position')).to_sym
        

        # Override the default ids setter, first setting the correct position
        # on each relation based on the index of its id in the given array.
        #
        # @override model#{name}_ids= 
        before_method self, "#{name.to_s.singularize}_ids=" do |ids|
          ids.each_with_index do |id, index|
            meta.klass.find(id).update_attribute field_name, index + 1
          end

          meta.klass.where(meta.foreign_key => id).not_in(id: ids).each do |obj|
            obj.update_attribute field_name, nil
          end
        end

        meta[:order] ||= "#{field_name} asc"
      end

      private

      # Redefines a method on owner to first execute &block, then
      # continue executing original method. 
      #
      # @param [ Object ] owner  The owner of the method
      # @param [ String ] method The name of the method to override
      # @param [ Proc ]   &block The block to execute before original method
      #
      # @private
      # @since 0.0.3
      def before_method owner, method, &block
        original_method = instance_method method
        owner.re_define_method method do |*args|
          block.call *args
          original_method.bind(self).call *args
        end
      end

    end

  end

end
{'fraction_non_alphanumeric': 0.08026943586865001, 'fraction_numerical': 0.0030872859949480774, 'mean_word_length': 2.1849865951742626, 'pattern_counts': {'":': 0, '<': 3, '<?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': '3583821', 'n_tokens_mistral': 1016, 'n_tokens_neox': 979, 'n_words': 320}