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: {}) ⇒ 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



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

def initialize(
  template_analysis: nil,
  dest_analysis: nil,
  conflicts: [],
  frozen_blocks: [],
  stats: {}
)
  @template_analysis = template_analysis
  @dest_analysis = dest_analysis
  @lines = []
  @decisions = []
  @conflicts = conflicts
  @frozen_blocks = frozen_blocks
  @stats = stats
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



99
100
101
# File 'lib/ast/merge/merge_result_base.rb', line 99

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



107
108
109
# File 'lib/ast/merge/merge_result_base.rb', line 107

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

#content?Boolean

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

Returns:

  • (Boolean)


123
124
125
# File 'lib/ast/merge/merge_result_base.rb', line 123

def content?
  !@lines.empty?
end

#decision_summaryHash<Symbol, Integer>

Get summary of decisions made

Returns:

  • (Hash<Symbol, Integer>)


141
142
143
144
145
# File 'lib/ast/merge/merge_result_base.rb', line 141

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)


129
130
131
# File 'lib/ast/merge/merge_result_base.rb', line 129

def empty?
  @lines.empty?
end

#inspectString

String representation

Returns:

  • (String)


149
150
151
# File 'lib/ast/merge/merge_result_base.rb', line 149

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

#line_countInteger

Get the number of lines

Returns:

  • (Integer)


135
136
137
# File 'lib/ast/merge/merge_result_base.rb', line 135

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



116
117
118
# File 'lib/ast/merge/merge_result_base.rb', line 116

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