Class: Psych::Merge::PartialTemplateMerger
- Inherits:
-
Object
- Object
- Psych::Merge::PartialTemplateMerger
- 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:
- Finds a specific key path in the destination (e.g., [“AllCops”, “Exclude”])
- Merges template content at that location
- Leaves the rest of the destination unchanged
Defined Under Namespace
Classes: Result
Instance Attribute Summary collapse
-
#add_missing ⇒ Boolean
readonly
Whether to add template items not in destination.
-
#destination ⇒ String
readonly
The destination content.
-
#key_path ⇒ Array<String, Integer>
readonly
Path to the target key (e.g., [“AllCops”, “Exclude”]).
-
#preference ⇒ Symbol
readonly
Merge preference (:template or :destination).
-
#recursive ⇒ Boolean
readonly
Whether to recursively merge nested structures.
-
#remove_missing ⇒ Boolean
readonly
Whether to remove destination items not in template.
-
#template ⇒ String
readonly
The template content to merge.
-
#when_missing ⇒ Symbol
readonly
What to do when key path not found (:skip, :add).
Instance Method Summary collapse
-
#initialize(template:, destination:, key_path:, preference: :destination, add_missing: true, remove_missing: false, when_missing: :skip, recursive: true) ⇒ PartialTemplateMerger
constructor
Initialize a PartialTemplateMerger.
-
#merge ⇒ Result
Perform the partial template merge.
Constructor Details
#initialize(template:, destination:, key_path:, preference: :destination, add_missing: true, remove_missing: false, when_missing: :skip, recursive: true) ⇒ PartialTemplateMerger
Initialize a PartialTemplateMerger.
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/psych/merge/partial_template_merger.rb', line 92 def initialize( template:, destination:, key_path:, preference: :destination, add_missing: true, remove_missing: false, when_missing: :skip, recursive: true ) @template = template @destination = destination @key_path = Array(key_path) @preference = preference @add_missing = add_missing @remove_missing = remove_missing @when_missing = when_missing @recursive = recursive validate_key_path! end |
Instance Attribute Details
#add_missing ⇒ Boolean (readonly)
Returns Whether to add template items not in destination.
71 72 73 |
# File 'lib/psych/merge/partial_template_merger.rb', line 71 def add_missing @add_missing end |
#destination ⇒ String (readonly)
Returns The destination content.
62 63 64 |
# File 'lib/psych/merge/partial_template_merger.rb', line 62 def destination @destination end |
#key_path ⇒ Array<String, Integer> (readonly)
Returns Path to the target key (e.g., [“AllCops”, “Exclude”]).
65 66 67 |
# File 'lib/psych/merge/partial_template_merger.rb', line 65 def key_path @key_path end |
#preference ⇒ Symbol (readonly)
Returns Merge preference (:template or :destination).
68 69 70 |
# File 'lib/psych/merge/partial_template_merger.rb', line 68 def preference @preference end |
#recursive ⇒ Boolean (readonly)
Returns Whether to recursively merge nested structures.
80 81 82 |
# File 'lib/psych/merge/partial_template_merger.rb', line 80 def recursive @recursive end |
#remove_missing ⇒ Boolean (readonly)
Returns Whether to remove destination items not in template.
74 75 76 |
# File 'lib/psych/merge/partial_template_merger.rb', line 74 def remove_missing @remove_missing end |
#template ⇒ String (readonly)
Returns The template content to merge.
59 60 61 |
# File 'lib/psych/merge/partial_template_merger.rb', line 59 def template @template end |
#when_missing ⇒ Symbol (readonly)
Returns What to do when key path not found (:skip, :add).
77 78 79 |
# File 'lib/psych/merge/partial_template_merger.rb', line 77 def when_missing @when_missing end |
Instance Method Details
#merge ⇒ Result
Perform the partial template merge.
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/psych/merge/partial_template_merger.rb', line 117 def merge d_analysis = FileAnalysis.new(destination) unless d_analysis.valid? return Result.new( content: destination, has_key_path: false, changed: false, message: "Failed to parse destination: #{d_analysis.errors.join(", ")}", ) end # Navigate to the key path target_entry = find_key_path(d_analysis) if target_entry.nil? return handle_missing_key_path(d_analysis) end # Perform the merge at the found location perform_merge_at_path(d_analysis, target_entry) end |