Class: Ast::Merge::SmartMergerBase Abstract
- Inherits:
-
Object
- Object
- Ast::Merge::SmartMergerBase
- Includes:
- RegionMergeable
- Defined in:
- lib/ast/merge/smart_merger_base.rb
Overview
Subclass and implement #analysis_class and #perform_merge
Abstract base class for SmartMerger implementations across all *-merge gems.
SmartMergerBase provides the standard interface and common functionality for intelligent file merging. Subclasses implement format-specific parsing, analysis, and merge logic while inheriting the common API.
## Standard Options
All SmartMerger implementations support these common options:
-
‘preference` - `:destination` (default) or `:template`
-
‘add_template_only_nodes` - `false` (default) or `true`
-
‘signature_generator` - Custom signature proc or `nil`
-
‘freeze_token` - Token for freeze block markers
-
‘match_refiner` - Fuzzy match refiner or `nil`
-
‘regions` - Region configurations for nested merging
-
‘region_placeholder` - Custom placeholder for regions
## Implementing a SmartMerger
Subclasses must implement:
-
‘analysis_class` - Returns the FileAnalysis class for this format
-
‘perform_merge` - Performs the format-specific merge logic
Subclasses may override:
-
‘default_freeze_token` - Format-specific default freeze token
-
‘resolver_class` - Returns the ConflictResolver class (if different)
-
‘result_class` - Returns the MergeResult class (if different)
-
‘aligner_class` - Returns the FileAligner class (if used)
-
‘parse_content` - Custom parsing logic
-
‘build_analysis_options` - Additional analysis options
-
‘build_resolver_options` - Additional resolver options
Direct Known Subclasses
Constant Summary
Constants included from RegionMergeable
RegionMergeable::DEFAULT_PLACEHOLDER_PREFIX, RegionMergeable::DEFAULT_PLACEHOLDER_SUFFIX
Instance Attribute Summary collapse
-
#add_template_only_nodes ⇒ Boolean
readonly
Whether to add template-only nodes.
-
#aligner ⇒ Object?
readonly
Aligner for finding matches (if applicable).
-
#dest_analysis ⇒ Object
readonly
Analysis of the destination file.
-
#dest_content ⇒ String
readonly
Destination source content.
-
#freeze_token ⇒ String
readonly
Token for freeze block markers.
-
#match_refiner ⇒ Object?
readonly
Match refiner for fuzzy matching.
-
#preference ⇒ Symbol, Hash
readonly
Preference for signature matches.
-
#resolver ⇒ Object
readonly
Resolver for handling conflicts.
-
#result ⇒ Object
readonly
Result object tracking merged content.
-
#signature_generator ⇒ Proc?
readonly
Custom signature generator.
-
#template_analysis ⇒ Object
readonly
Analysis of the template file.
-
#template_content ⇒ String
readonly
Template source content.
Instance Method Summary collapse
-
#initialize(template_content, dest_content, signature_generator: nil, preference: :destination, add_template_only_nodes: false, freeze_token: nil, match_refiner: nil, regions: nil, region_placeholder: nil, **format_options) ⇒ SmartMergerBase
constructor
Creates a new SmartMerger for intelligent file merging.
-
#merge ⇒ String
Perform the merge operation and return the merged content as a string.
-
#merge_result ⇒ Object
Perform the merge operation and return the full result object.
-
#merge_with_debug ⇒ Hash
Perform the merge and return detailed debug information.
-
#stats ⇒ Hash
Get merge statistics.
Methods included from RegionMergeable
#extract_dest_regions, #extract_template_regions, #regions_configured?, #setup_regions, #substitute_merged_regions
Constructor Details
#initialize(template_content, dest_content, signature_generator: nil, preference: :destination, add_template_only_nodes: false, freeze_token: nil, match_refiner: nil, regions: nil, region_placeholder: nil, **format_options) ⇒ SmartMergerBase
Creates a new SmartMerger for intelligent file merging.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/ast/merge/smart_merger_base.rb', line 145 def initialize( template_content, dest_content, signature_generator: nil, preference: :destination, add_template_only_nodes: false, freeze_token: nil, match_refiner: nil, regions: nil, region_placeholder: nil, ** ) @template_content = template_content @dest_content = dest_content @signature_generator = signature_generator @preference = preference @add_template_only_nodes = add_template_only_nodes @freeze_token = freeze_token || default_freeze_token @match_refiner = match_refiner @format_options = # Set up region support setup_regions(regions: regions || [], region_placeholder: region_placeholder) # Extract regions before parsing (if configured) template_for_parsing = extract_template_regions(@template_content) dest_for_parsing = extract_dest_regions(@dest_content) # Parse and analyze both files @template_analysis = parse_and_analyze(template_for_parsing, :template) @dest_analysis = parse_and_analyze(dest_for_parsing, :destination) # Set up aligner (if applicable) @aligner = build_aligner if respond_to?(:aligner_class, true) && aligner_class # Set up resolver @resolver = build_resolver # Set up result @result = build_result end |
Instance Attribute Details
#add_template_only_nodes ⇒ Boolean (readonly)
Returns Whether to add template-only nodes.
87 88 89 |
# File 'lib/ast/merge/smart_merger_base.rb', line 87 def add_template_only_nodes @add_template_only_nodes end |
#aligner ⇒ Object? (readonly)
Returns Aligner for finding matches (if applicable).
75 76 77 |
# File 'lib/ast/merge/smart_merger_base.rb', line 75 def aligner @aligner end |
#dest_analysis ⇒ Object (readonly)
Returns Analysis of the destination file.
72 73 74 |
# File 'lib/ast/merge/smart_merger_base.rb', line 72 def dest_analysis @dest_analysis end |
#dest_content ⇒ String (readonly)
Returns Destination source content.
66 67 68 |
# File 'lib/ast/merge/smart_merger_base.rb', line 66 def dest_content @dest_content end |
#freeze_token ⇒ String (readonly)
Returns Token for freeze block markers.
90 91 92 |
# File 'lib/ast/merge/smart_merger_base.rb', line 90 def freeze_token @freeze_token end |
#match_refiner ⇒ Object? (readonly)
Returns Match refiner for fuzzy matching.
96 97 98 |
# File 'lib/ast/merge/smart_merger_base.rb', line 96 def match_refiner @match_refiner end |
#preference ⇒ Symbol, Hash (readonly)
Returns Preference for signature matches.
84 85 86 |
# File 'lib/ast/merge/smart_merger_base.rb', line 84 def preference @preference end |
#resolver ⇒ Object (readonly)
Returns Resolver for handling conflicts.
78 79 80 |
# File 'lib/ast/merge/smart_merger_base.rb', line 78 def resolver @resolver end |
#result ⇒ Object (readonly)
Returns Result object tracking merged content.
81 82 83 |
# File 'lib/ast/merge/smart_merger_base.rb', line 81 def result @result end |
#signature_generator ⇒ Proc? (readonly)
Returns Custom signature generator.
93 94 95 |
# File 'lib/ast/merge/smart_merger_base.rb', line 93 def signature_generator @signature_generator end |
#template_analysis ⇒ Object (readonly)
Returns Analysis of the template file.
69 70 71 |
# File 'lib/ast/merge/smart_merger_base.rb', line 69 def template_analysis @template_analysis end |
#template_content ⇒ String (readonly)
Returns Template source content.
63 64 65 |
# File 'lib/ast/merge/smart_merger_base.rb', line 63 def template_content @template_content end |
Instance Method Details
#merge ⇒ String
Perform the merge operation and return the merged content as a string.
190 191 192 |
# File 'lib/ast/merge/smart_merger_base.rb', line 190 def merge merge_result.to_s end |
#merge_result ⇒ Object
Perform the merge operation and return the full result object.
This method is memoized - subsequent calls return the cached result.
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/ast/merge/smart_merger_base.rb', line 199 def merge_result return @merge_result if @merge_result @merge_result = DebugLogger.time("#{self.class.name}#merge") do result = perform_merge # Substitute merged regions back into the result if configured if regions_configured? && (merged_content = result.to_s) final_content = substitute_merged_regions(merged_content) update_result_content(result, final_content) end result end end |
#merge_with_debug ⇒ Hash
Perform the merge and return detailed debug information.
220 221 222 223 224 225 226 227 |
# File 'lib/ast/merge/smart_merger_base.rb', line 220 def merge_with_debug content = merge { content: content, statistics: @result.decision_summary, } end |
#stats ⇒ Hash
Get merge statistics.
232 233 234 235 |
# File 'lib/ast/merge/smart_merger_base.rb', line 232 def stats merge_result # Ensure merge has run @result.decision_summary end |