Class: Ast::Merge::MergeResultBase

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

Overview

Base class for tracking merge results in AST merge libraries.
Provides shared decision constants and base functionality for
file-type-specific implementations.

Examples:

Basic usage in a subclass

class MyMergeResult < Ast::Merge::MergeResultBase
  def add_node(node, decision:, source:)
    # File-type-specific node handling
  end
end

Direct Known Subclasses

Text::MergeResult

Constant Summary collapse

DECISION_KEPT_TEMPLATE =

Line was kept from template (no conflict or template preferred).
Used when template content is included without modification.

:kept_template
DECISION_KEPT_DEST =

Line was kept from destination (no conflict or destination preferred).
Used when destination content is included without modification.

:kept_destination
DECISION_MERGED =

Line was merged from both sources.
Used when content was combined from template and destination.

:merged
DECISION_ADDED =

Line was added from template (template-only content).
Used for content that exists only in template and is added to result.

:added
DECISION_FREEZE_BLOCK =

Line from destination freeze block (always preserved).
Used for content within freeze markers that must be kept
from destination regardless of template content.

:freeze_block
DECISION_REPLACED =

Line replaced matching content (signature match with preference applied).
Used when template and destination have nodes with same signature but
different content, and one version replaced the other based on preference.

:replaced
DECISION_APPENDED =

Line was appended from destination (destination-only content).
Used for content that exists only in destination and is added to result.

:appended

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_analysis: nil, dest_analysis: nil, conflicts: [], frozen_blocks: [], stats: {}, **options) ⇒ MergeResultBase

Initialize a new merge result.

This unified constructor accepts all parameters that any *-merge gem might need.
Subclasses should call super with the parameters they use.

Parameters:

  • template_analysis (Object, nil) (defaults to: nil)

    Analysis of the template file

  • dest_analysis (Object, nil) (defaults to: nil)

    Analysis of the destination file

  • conflicts (Array<Hash>) (defaults to: [])

    Conflicts detected during merge

  • frozen_blocks (Array) (defaults to: [])

    Frozen blocks preserved during merge

  • stats (Hash) (defaults to: {})

    Statistics about the merge

  • options (Hash)

    Additional options for forward compatibility



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/ast/merge/merge_result_base.rb', line 80

def initialize(
  template_analysis: nil,
  dest_analysis: nil,
  conflicts: [],
  frozen_blocks: [],
  stats: {},
  **options
)
  @template_analysis = template_analysis
  @dest_analysis = dest_analysis
  @lines = []
  @decisions = []
  @conflicts = conflicts
  @frozen_blocks = frozen_blocks
  @stats = stats
  # **options captured for forward compatibility - subclasses may use additional options
end

Instance Attribute Details

#conflictsArray<Hash> (readonly)

Returns Conflicts detected during merge.

Returns:

  • (Array<Hash>)

    Conflicts detected during merge



61
62
63
# File 'lib/ast/merge/merge_result_base.rb', line 61

def conflicts
  @conflicts
end

#decisionsArray<Hash> (readonly)

Returns Decisions made during merge.

Returns:

  • (Array<Hash>)

    Decisions made during merge



52
53
54
# File 'lib/ast/merge/merge_result_base.rb', line 52

def decisions
  @decisions
end

#dest_analysisObject? (readonly)

Returns Analysis of the destination file.

Returns:

  • (Object, nil)

    Analysis of the destination file



58
59
60
# File 'lib/ast/merge/merge_result_base.rb', line 58

def dest_analysis
  @dest_analysis
end

#frozen_blocksArray (readonly)

Returns Frozen blocks preserved during merge.

Returns:

  • (Array)

    Frozen blocks preserved during merge



64
65
66
# File 'lib/ast/merge/merge_result_base.rb', line 64

def frozen_blocks
  @frozen_blocks
end

#linesArray<String> (readonly)

Returns Lines in the result (canonical storage for line-by-line merging).

Returns:

  • (Array<String>)

    Lines in the result (canonical storage for line-by-line merging)



49
50
51
# File 'lib/ast/merge/merge_result_base.rb', line 49

def lines
  @lines
end

#statsHash (readonly)

Returns Statistics about the merge.

Returns:

  • (Hash)

    Statistics about the merge



67
68
69
# File 'lib/ast/merge/merge_result_base.rb', line 67

def stats
  @stats
end

#template_analysisObject? (readonly)

Returns Analysis of the template file.

Returns:

  • (Object, nil)

    Analysis of the template file



55
56
57
# File 'lib/ast/merge/merge_result_base.rb', line 55

def template_analysis
  @template_analysis
end

Instance Method Details

#contentArray<String>

Get content - returns @lines array for most gems.
Subclasses may override for different content models (e.g., string).

Returns:

  • (Array<String>)

    The merged content as array of lines



102
103
104
# File 'lib/ast/merge/merge_result_base.rb', line 102

def content
  @lines
end

#content=(value) ⇒ Object

Set content from a string (splits on newlines).
Used when region substitution replaces the merged content.

Parameters:

  • value (String)

    The new content



110
111
112
# File 'lib/ast/merge/merge_result_base.rb', line 110

def content=(value)
  @lines = value.to_s.split("\n", -1)
end

#content?Boolean

Check if content has been built (has any lines).

Returns:

  • (Boolean)


126
127
128
# File 'lib/ast/merge/merge_result_base.rb', line 126

def content?
  !@lines.empty?
end

#decision_summaryHash<Symbol, Integer>

Get summary of decisions made

Returns:

  • (Hash<Symbol, Integer>)


144
145
146
147
148
# File 'lib/ast/merge/merge_result_base.rb', line 144

def decision_summary
  summary = Hash.new(0)
  @decisions.each { |d| summary[d[:decision]] += 1 }
  summary
end

#empty?Boolean

Check if the result is empty

Returns:

  • (Boolean)


132
133
134
# File 'lib/ast/merge/merge_result_base.rb', line 132

def empty?
  @lines.empty?
end

#inspectString

String representation

Returns:

  • (String)


152
153
154
# File 'lib/ast/merge/merge_result_base.rb', line 152

def inspect
  "#<#{self.class.name} lines=#{line_count} decisions=#{@decisions.length}>"
end

#line_countInteger

Get the number of lines

Returns:

  • (Integer)


138
139
140
# File 'lib/ast/merge/merge_result_base.rb', line 138

def line_count
  @lines.length
end

#to_sString

Get content as a string.
This is the canonical method for converting the merge result to a string.
Subclasses may override to customize string output (e.g., adding trailing newline).

Returns:

  • (String)

    Content as string joined with newlines



119
120
121
# File 'lib/ast/merge/merge_result_base.rb', line 119

def to_s
  @lines.join("\n")
end