Class: Ast::Merge::KeyPathPartialTemplateMergerBase

Inherits:
Object
  • Object
show all
Defined in:
lib/ast/merge/key_path_partial_template_merger_base.rb

Overview

Base class for merging a partial template into a specific key path of a structured destination document.

Unlike the navigable section-based PartialTemplateMergerBase, this base:

  1. Navigates a nested key path in a structured document
  2. Merges only the value located at that path
  3. Leaves the rest of the destination unchanged

Ownership boundary:

  • this base owns the shared control flow for key-path navigation, missing-path insertion, and full-document wrapper construction
  • concrete subclasses own parser-specific analysis, child traversal, serialization, and SmartMerger integration

Expected duck-type surface from analyses and entries:

  • analysis responds to #valid?, #errors, and #statements
  • entries respond to #key_name, #mapping?, #sequence?, and #scalar?

Defined Under Namespace

Classes: Result

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template:, destination:, key_path:, preference: :destination, add_missing: true, remove_missing: false, when_missing: :skip, recursive: true) ⇒ KeyPathPartialTemplateMergerBase

Returns a new instance of KeyPathPartialTemplateMergerBase.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/ast/merge/key_path_partial_template_merger_base.rb', line 43

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_missingObject (readonly)

Returns the value of attribute add_missing.



40
41
42
# File 'lib/ast/merge/key_path_partial_template_merger_base.rb', line 40

def add_missing
  @add_missing
end

#destinationObject (readonly)

Returns the value of attribute destination.



40
41
42
# File 'lib/ast/merge/key_path_partial_template_merger_base.rb', line 40

def destination
  @destination
end

#key_pathObject (readonly)

Returns the value of attribute key_path.



40
41
42
# File 'lib/ast/merge/key_path_partial_template_merger_base.rb', line 40

def key_path
  @key_path
end

#preferenceObject (readonly)

Returns the value of attribute preference.



40
41
42
# File 'lib/ast/merge/key_path_partial_template_merger_base.rb', line 40

def preference
  @preference
end

#recursiveObject (readonly)

Returns the value of attribute recursive.



40
41
42
# File 'lib/ast/merge/key_path_partial_template_merger_base.rb', line 40

def recursive
  @recursive
end

#remove_missingObject (readonly)

Returns the value of attribute remove_missing.



40
41
42
# File 'lib/ast/merge/key_path_partial_template_merger_base.rb', line 40

def remove_missing
  @remove_missing
end

#templateObject (readonly)

Returns the value of attribute template.



40
41
42
# File 'lib/ast/merge/key_path_partial_template_merger_base.rb', line 40

def template
  @template
end

#when_missingObject (readonly)

Returns the value of attribute when_missing.



40
41
42
# File 'lib/ast/merge/key_path_partial_template_merger_base.rb', line 40

def when_missing
  @when_missing
end

Instance Method Details

#mergeResult

Merge template content into the configured key path.

Returns:



68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ast/merge/key_path_partial_template_merger_base.rb', line 68

def merge
  analysis = create_analysis(destination)

  unless analysis.valid?
    return Result.new(
      content: destination,
      has_key_path: false,
      changed: false,
      message: "Failed to parse destination: #{Array(analysis.errors).join(', ')}"
    )
  end

  target_entry = find_key_path(analysis)
  return handle_missing_key_path if target_entry.nil?

  perform_merge_at_path(target_entry)
end