<commit_before>module Mongoid
  module Alize
    class ToCallback < Callback

      def attach
        define_denorm_attrs

        define_callback
        alias_callback
        set_callback

        define_destroy_callback
        alias_destroy_callback
        set_destroy_callback
      end

      def define_callback
        klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1

          def #{callback_name}#{force_param}
            #{iterable_relation}.each do |relation|
              next if relation.attributes.frozen?

              is_one = #{is_one?}
              if is_one
                field_values = #{field_values("self")}
              else
                field_values = #{field_values("self", :id => true)}
              end

              prefixed_name = #{prefixed_name}
              if is_one
                #{relation_set('prefixed_name', 'field_values')}
              else
                #{pull_from_inverse}
                #{relation_push('prefixed_name', 'field_values')}
              end

            end

            #{debug ? "puts \"#{callback_name}\"": ""}
            true
          end
          protected :#{callback_name}
        CALLBACK
      end

      def define_destroy_callback
        klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1

          def #{destroy_callback_name}
            #{iterable_relation}.each do |relation|
              next if relation.attributes.frozen?

              is_one = #{is_one?}
              prefixed_name = #{prefixed_name}
              if is_one
                #{relation_set('prefixed_name', 'nil')}
              else
                #{pull_from_inverse}
              end
            end

            #{debug ? "puts \"#{destroy_callback_name}\"": ""}
            true
          end
          protected :#{destroy_callback_name}
        CALLBACK
      end

      def pull_from_inverse
        <<-RUBIES
          #{relation_pull('prefixed_name', '{ "_id" => self.id }')}
          if _f = relation.send(prefixed_name)
            _f.reject! do |hash|
              hash["_id"] == self.id
            end
          end
        RUBIES
      end

      def prefixed_name
        if inverse_relation
          ":#{inverse_relation}_fields"<commit_msg>looking at both _id and id because the serialization is using id<commit_after>module Mongoid
  module Alize
    class ToCallback < Callback

      def attach
        define_denorm_attrs

        define_callback
        alias_callback
        set_callback

        define_destroy_callback
        alias_destroy_callback
        set_destroy_callback
      end

      def define_callback
        klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1

          def #{callback_name}#{force_param}
            #{iterable_relation}.each do |relation|
              next if relation.attributes.frozen?

              is_one = #{is_one?}
              if is_one
                field_values = #{field_values("self")}
              else
                field_values = #{field_values("self", :id => true)}
              end

              prefixed_name = #{prefixed_name}
              if is_one
                #{relation_set('prefixed_name', 'field_values')}
              else
                #{pull_from_inverse}
                #{relation_push('prefixed_name', 'field_values')}
              end

            end

            #{debug ? "puts \"#{callback_name}\"": ""}
            true
          end
          protected :#{callback_name}
        CALLBACK
      end

      def define_destroy_callback
        klass.class_eval <<-CALLBACK, __FILE__, __LINE__ + 1

          def #{destroy_callback_name}
            #{iterable_relation}.each do |relation|
              next if relation.attributes.frozen?

              is_one = #{is_one?}
              prefixed_name = #{prefixed_name}
              if is_one
                #{relation_set('prefixed_name', 'nil')}
              else
                #{pull_from_inverse}
              end
            end

            #{debug ? "puts \"#{destroy_callback_name}\"": ""}
            true
          end
          protected :#{destroy_callback_name}
        CALLBACK
      end

      def pull_from_inverse
        <<-RUBIES
          #{relation_pull('prefixed_name', '{"$or" => [{ "_id" => self.id }, { "id" => self.id }]}')}
          if _f = relation.send(prefixed_name)
            _f.reject! do |hash|
              hash["_id"] == self.id || hash["id"] == self.id
            end
          end
        RUBIES
      end

      def prefixed_name
        if inverse_relation
          ":#{inverse_relation}_fields"
{'fraction_non_alphanumeric': 0.09260869565217392, 'fraction_numerical': 0.0008695652173913044, 'mean_word_length': 1.5100927441352974, 'pattern_counts': {'":': 6, '<': 17, '<?xml version=': 0, '>': 9, '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': '6252866', 'n_tokens_mistral': 1283, 'n_tokens_neox': 1254, 'n_words': 263}