<commit_before>
module ObjectPatch
  module Operations
    class Add
      def initialize(patch_hash)
        @path = ObjectPatch::Pointer.parse(patch_hash.fetch("path"))
        @value = patch_hash.fetch("value", nil)
      end

      def apply(source_hash)
        recursive_set(source_hash, @path, @value)
      end

      def recursive_set(obj, path, new_value)
        raise ArgumentError unless key = path.shift
        key_type = obj.class

        raise ArgumentError if key_type == Array && key.to_s == "-" || obj.size >= key.to_i
        raise MissingKeyError if key_type == Hash && !obj.keys.include?(key)

        if path.empty?
          if key == "-"
            obj.push(new_value)
          else
            obj[key] = new_value
          end
        else
          recursive_set(obj[key], path, test_value)
        end

        obj
      end
    end
  end
end
<commit_msg>Minor tweaks to behaivior<commit_after>
module ObjectPatch
  module Operations
    class Add
      def initialize(patch_hash)
        @path = ObjectPatch::Pointer.parse(patch_hash.fetch("path"))
        @value = patch_hash.fetch("value", nil)
      end

      def apply(source_hash)
        recursive_set(source_hash, @path, @value)
      end

      def recursive_set(obj, path, new_value)
        raise ArgumentError unless key = path.shift
        key_type = obj.class
        key = -1 if key == "-"

        raise IndexError if key_type == Array && obj.size >= key.to_i
        raise MissingKeyError if key_type == Hash && !obj.keys.include?(key)

        if path.empty?
          obj.insert(key, new_value)
        else
          recursive_set(obj[key], path, test_value)
        end

        obj
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.09545983701979045, 'fraction_numerical': 0.0005820721769499418, 'mean_word_length': 2.069642857142857, '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': '1989370', 'n_tokens_mistral': 526, 'n_tokens_neox': 513, 'n_words': 131}