Class: Psych::Merge::PartialTemplateMerger

Inherits:
Ast::Merge::KeyPathPartialTemplateMergerBase
  • Object
show all
Defined in:
lib/psych/merge/partial_template_merger.rb

Overview

Merges a partial YAML template into a specific key path of a destination document.

Unlike the full SmartMerger which merges entire documents, PartialTemplateMerger:

  1. Finds a specific key path in the destination (e.g., ["AllCops", "Exclude"])
  2. Merges template content at that location
  3. Leaves the rest of the destination unchanged

Examples:

Basic usage - merge into existing key

template = <<~YAML
  - examples/**/*
  - vendor/**/*
YAML

destination = <<~YAML
  AllCops:
    Exclude:
      - tmp/**/*
    TargetRubyVersion: 3.2
YAML

merger = PartialTemplateMerger.new(
  template: template,
  destination: destination,
  key_path: ["AllCops", "Exclude"]
)
result = merger.merge

Adding a new nested key

merger = PartialTemplateMerger.new(
  template: "enable",
  destination: "AllCops:\n  Exclude: []",
  key_path: ["AllCops", "NewCops"],
  when_missing: :add
)